`
feiliboos
  • 浏览: 665247 次
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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

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

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

  1. /**//// <summary>
  2. /// 抽象人类
  3. /// </summary>
  4. public abstract class Person
  5. {
  6. /**//// <summary>
  7. /// 使用工具劳动
  8. /// </summary>
  9. public abstract void Work();
  10. }
  11. public interface ITool
  12. {
  13. /**//// <summary>
  14. /// 使用工具
  15. /// </summary>
  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. /**//// <summary>
  4. /// 原始社会使用长矛打猎
  5. /// </summary>
  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. /**//// <summary>
  4. /// 工厂制造工具
  5. /// </summary>
  6. /// <returns></returns>
  7. public static ITool CreateTool()
  8. {
  9. return new Hoe(); // 制造锄头
  10. }
  11. }
复制代码

EconomyPerson

  1. public class EconomyPerson : Person
  2. {
  3. /**//// <summary>
  4. /// 经济社会使用锄头耕作
  5. /// </summary>
  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. /**//// <summary>
  4. /// 从外部获取工具
  5. /// </summary>
  6. public ITool Tool { get; set; }
  7. /**//// <summary>
  8. /// 现在人用不需要知道电脑是哪来的,直接拿来办公
  9. /// </summary>
  10. public override void Work()
  11. {
  12. //不知道使用什么工具和哪来的工具,只是机械化的办公
  13. Tool.UseTool();
  14. Console.WriteLine("使用工具办公");
  15. }
  16. }
复制代码
App.config
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <sectionGroup name="spring">
  5. <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
  6. <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
  7. </sectionGroup>
  8. </configSections>
  9. <spring>
  10. <context>
  11. <resource uri="config://spring/objects" />
  12. </context>
  13. <objects xmlns="http://www.springframework.net">
  14. <description>一个简单的控制反转例子</description>
  15. <object id="computer" type="SpringNetIoC.Computer, SpringNetIoC" />
  16. <object id="modernPerson" type="SpringNetIoC.ModernPerson, SpringNetIoC">
  17. <property name="Tool" ref="computer"/>
  18. </object>
  19. </objects>
  20. </spring>
  21. </configuration>
复制代码
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容器管理,这样可以实现动态的拆装组建和组件重用。我个人理解依赖注入是反射工厂的加强版。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics