Spring.Net学习笔记五(依赖注入)

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

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

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

    

     /**//// <summary>
    /// 抽象人类
    /// </summary>
    public abstract class Person
    {
        /**//// <summary>
        /// 使用工具劳动
        /// </summary>
        public abstract void Work();
    }
    public interface ITool
    {
        /**//// <summary>
        /// 使用工具
        /// </summary>
        void UseTool();
    }

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

    public class Spear : ITool
    {
        public void UseTool()
        {
            Console.WriteLine("使用长矛");
        }
    }

  PrimitivePerson

    public class PrimitivePerson : Person
    {
        /**//// <summary>
        /// 原始社会使用长矛打猎
        /// </summary>
        public override void Work()
        {
            //知道打猎使用的是长矛,并且制造长矛
            ITool tool = new Spear();
            tool.UseTool();
            Console.WriteLine("使用长矛打猎");
        }
    }

    Spring.NET教程(六)——依赖注入(应用篇)

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

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

    public class Hoe : ITool
    {
        public void UseTool()
        {
            Console.WriteLine("使用锄头");
        }
    }

     ToolFactory

    public static class ToolFactory
    {
        /**//// <summary>
        /// 工厂制造工具
        /// </summary>
        /// <returns></returns>
        public static ITool CreateTool()
        {
            return new Hoe();  // 制造锄头
        }
    }

  EconomyPerson

    public class EconomyPerson : Person
    {
        /**//// <summary>
        /// 经济社会使用锄头耕作
        /// </summary>
        public override void Work()
        {
            //不用知道什么工具,只需知道工厂能买到工具,而不自己制造工具,但仅由工厂制造锄头
            ITool tool = ToolFactory.CreateTool();
            tool.UseTool();
            Console.WriteLine("经济社会使用工具耕作");
        }
    }

    Spring.NET教程(六)——依赖注入(应用篇)

  从上面代码我可以看出:运用的经典的工厂模式, EconomyPerson仅仅对工厂耦合,并不关心工厂是怎样制造工具。

       

 1 namespace Dao.IOC
 2 {
 3     public abstract class Person
 4     {
 5         /// <summary>
 6         /// 使用工具劳动
 7         /// </summary>
 8         public abstract void Work();
 9     }
10 
11     public class ModernPerson : Person
12     {
13         /**/
14         /// <summary>
15         /// 从外部获取工具
16         /// </summary>
17         public ITool Tool { get; set; }
18         /**/
19         /// <summary>
20         /// 现在人用不需要知道电脑是哪来的,直接拿来办公
21         /// </summary>
22         public override void Work()
23         {
24             //不知道使用什么工具和哪来的工具,只是机械化的办公
25             Tool.UseTool();
26             Console.WriteLine("使用工具办公");
27         }
28     }
29 }
 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       <!--容器配置-->
12       <resource uri="config://spring/objects"/>
13     </context>
14     <objects xmlns="http://www.springframework.net">
15       <!--这里放容器里面的所有节点-->
16       <description>An  example that demonstrates simple IoC features.</description>
17       <!--name 必须要唯一的,type=类的全名称,所在的程序集-->
18       <object name="computer" type="Dao.IOC.Computer,Dao">
19       </object>
20       <object name="modernPerson" type="Dao.IOC.ModernPerson, Dao">
21         <property name="Tool" ref="computer"/>
22       </object>
23     </objects>
24   </spring>
25   <startup>
26     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
27   </startup>
28 </configuration>
1  static void Main(string[] args)
2         {
3 
4             IApplicationContext ctx = ContextRegistry.GetContext();
5             Person person = (Person)ctx.GetObject("modernPerson");
6             person.Work();
7             Console.ReadLine();
8         }

     

  从上面代码我们可以看出,把对象交给Spring.NET容器进行管理,ModernPerson类不需要知道具体使用什么工具,仅仅是机械化的工作。至于使用的什么工具,则由配置文件决定,所有对象由Spring.NET容器管理,这样可以实现动态的拆装组建和组件重用。我个人理解依赖注入是反射工厂的加强版。

posted @ 2015-08-05 18:12  王永华  阅读(520)  评论(0编辑  收藏  举报