Spring.NET学习笔记6——依赖注入(应用篇)

谈到高级语言编程,我们就会联想到设计模式;谈到设计模式,我们就会说道怎么样解耦合。而Spring.NET的IoC容器其中的一种用途就是解耦合,其最经典的应用就是:依赖注入(Dependeny Injection)简称DI,目前DI是最优秀的解耦方式之一。下面我就来谈谈依赖注入的应用场景。

  我模拟了三种不同的场景,可以一起学习使用依赖注入的重要性。

  下面是应用场景的条件:人类使用工具劳动。

  1.     /**
  2.     /// 抽象人类
  3.     ///
  4.     public abstract class Person
  5.     {
  6.         /**
  7.         /// 使用工具劳动
  8.         ///
  9.         public abstract void Work();
  10.     }
  11.     public interface ITool
  12.     {
  13.         /**
  14.         /// 使用工具
  15.         ///
  16.         void UseTool();
  17.     }
复制代码

场景一,原始社会:原始人使用长矛打猎

  1.     public class Spear : ITool
  2.     {
  3.         public void UseTool()
  4.         {
  5.             Console.WriteLine("使用长矛");
  6.         }
  7.     }
复制代码

PrimitivePerson

  1.     public class PrimitivePerson : Person
  2.     {
  3.         /**
  4.         /// 原始社会使用长矛打猎
  5.         ///
  6.         public override void Work()
  7.         {
  8.             //知道打猎使用的是长矛,并且制造长矛
  9.             ITool tool = new Spear();
  10.             tool.UseTool();
  11.             Console.WriteLine("使用长矛打猎");
  12.         }
  13.     }
复制代码

从上面代码我们不难看出,虽然使用的经典的里氏替换原则,但PrimitivePerson类于Spear类存在着耦合。

附件: 2009-10-26-1.GIF   

  场景二,经济社会:使用工具耕作

  1.     public class Hoe : ITool
  2.     {
  3.         public void UseTool()
  4.         {
  5.             Console.WriteLine("使用锄头");
  6.         }
  7.     }
复制代码

ToolFactory

  1.     public static class ToolFactory
  2.     {
  3.         /**
  4.         /// 工厂制造工具
  5.         ///
  6.         ///
  7.         public static ITool CreateTool()
  8.         {
  9.             return new Hoe();  // 制造锄头
  10.         }
  11.     }
复制代码

EconomyPerson

  1.     public class EconomyPerson : Person
  2.     {
  3.         /**
  4.         /// 经济社会使用锄头耕作
  5.         ///
  6.         public override void Work()
  7.         {
  8.             //不用知道什么工具,只需知道工厂能买到工具,而不自己制造工具,但仅由工厂制造锄头
  9.             ITool tool = ToolFactory.CreateTool();
  10.             tool.UseTool();
  11.             Console.WriteLine("经济社会使用工具耕作");
  12.         }
  13.     }
复制代码
从上面代码我可以看出:运用的经典的工厂模式, EconomyPerson仅仅对工厂耦合,并不关心工厂是怎样制造工具。

   场景三,现在社会:使用工具办公
  1.     public class Computer : ITool
  2.     {
  3.         public void UseTool()
  4.         {
  5.             Console.WriteLine("使用电脑");
  6.         }
  7.     }
复制代码
ModernPerson
  1.     public class ModernPerson : Person
  2.     {
  3.         /**
  4.         /// 从外部获取工具
  5.         ///
  6.         public ITool Tool { get; set; }
  7.         /**
  8.         /// 现在人用不需要知道电脑是哪来的,直接拿来办公
  9.         ///
  10.         public override void Work()
  11.         {
  12.             //不知道使用什么工具和哪来的工具,只是机械化的办公
  13.             Tool.UseTool();
  14.             Console.WriteLine("使用工具办公");
  15.         }
  16.     }
复制代码
App.config
  1.  
  2.    
  3.      
  4.      
  5.    
  6.  
  7.  
  8.    
  9.      
  10.    
  11.    
  12.       一个简单的控制反转例子
  13.      
  14.      
  15.        
  16.      
  17.    
  18.  
  19. 复制代码 Program
    1.     class Program
    2.     {
    3.         static void Main(string[] args)
    4.         {
    5.             IApplicationContext ctx = ContextRegistry.GetContext();
    6.             Person person = (Person)ctx.GetObject("modernPerson");
    7.             person.Work();
    8.             Console.ReadLine();
    9.         }
    10.     }
    复制代码
    从上面代码我们可以看出,把对象交给Spring.NET容器进行管理,ModernPerson类不需要知道具体使用什么工具,仅仅是机械化的工作。至于使用的什么工具,则由配置文件决定,所有对象由Spring.NET容器管理,这样可以实现动态的拆装组建和组件重用。我个人理解依赖注入是反射工厂的加强版。

你可能感兴趣的:(spring.net)