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

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

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

一.接口接受但是个直接耦合

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

二。工厂解耦 但代码改变的时候工厂里的代码要随着更改 不同业务要new 出许多的工厂仍然是个shit

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

2. public static class ToolFactory
    {
        /// <summary>
        /// 工厂制造工具
        /// </summary>
        /// <returns></returns>
        public static ITool CreateTool()
        {
            return new Hoe();  // 制造锄头
        }
    }
3.
    public class EconomyPerson : Person
    {
        /// <summary>
        /// 经济社会使用锄头耕作
        /// </summary>
        public override void Work()
        {
            //不用知道什么工具,只需知道工厂能买到工具,而不自己制造工具,但仅由工厂制造锄头
            ITool tool = ToolFactory.CreateTool();
            tool.UseTool();
            Console.WriteLine("经济社会使用工具耕作");
        }
    }
三.彻底解耦
1.   public class Computer : ITool
    {
        public void UseTool()
        {
            Console.WriteLine("使用电脑");
        }
    }

2.直接属性注入连工具是什么都不用知道 只提供接口 
public class ModernPerson : Person
    {
        /// <summary>
        /// 从外部获取工具
        /// </summary>
        public ITool Tool { get; set; }

        /// <summary>
        /// 现在人用不需要知道电脑是哪来的,直接拿来办公
        /// </summary>
        public override void Work()
        {
            //不知道使用什么工具和哪来的工具,只是机械化的办公
            Tool.UseTool();
            Console.WriteLine("使用工具办公");
        }
    }


    public interface ITool
    {
        /// <summary>
        /// 使用工具
        /// </summary>
        void UseTool();
    }

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

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

5.
    public class PrimitivePerson : Person
    {
        /// <summary>
        /// 原始社会使用长矛打猎
        /// </summary>
        public override void Work()
        {
            //知道打猎使用的是长矛,并且制造长矛
            ITool tool = new Spear(); 客户端与spear直接耦合就是个shit
            tool.UseTool();
            Console.WriteLine("使用长矛打猎");
        }
    }

6.使用配置文件解耦高级多了

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>

<spring>

<context>
      <resource uri="config://spring/objects" />
    </context>

<objects xmlns="http://www.springframework.net">
      <description>一个简单的控制反转例子</description>

<object id="computer" type="SpringNetIoC.Computer, SpringNetIoC" />

<object id="modernPerson" type="SpringNetIoC.ModernPerson, SpringNetIoC">
        <property name="Tool" ref="computer"/>
      </object>

</objects>

</spring>

</configuration>

7.找个人工作就行了 用什么工作已经看不出了
    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            Person person = (Person)ctx.GetObject("modernPerson");
            person.Work();

            Console.ReadLine();
        }
    }

Spring.NET学习笔记6——依赖注入(应用篇)的更多相关文章

  1. Spring.Net学习笔记(2)-依赖注入

    一.开发环境 操作系统:Win10 编译器:VS2013 framework版本:.net 4.5 Spring版本:1.3.1 二.涉及程序集 Spring.Core.dll Common.Logg ...

  2. SpringMVC:学习笔记(11)——依赖注入与@Autowired

    SpringMVC:学习笔记(11)——依赖注入与@Autowired 使用@Autowired 从Spring2.5开始,它引入了一种全新的依赖注入方式,即通过@Autowired注解.这个注解允许 ...

  3. Spring学习笔记1—依赖注入(构造器注入、set注入和注解注入)

    什么是依赖注入 在以前的java开发中,某个类中需要依赖其它类的方法时,通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,spring提出了依赖注入的思想,即依赖类不由程序员 ...

  4. AngularJS学习笔记之依赖注入

    最近在看AngularJS权威指南,由于各种各样的原因(主要是因为我没有money,好讨厌的有木有......),于是我选择了网上下载电子版的(因为它不要钱,哈哈...),字体也蛮清晰的,总体效果还不 ...

  5. Unity学习笔记(4):依赖注入

    Unity具体实现依赖注入包含构造函数注入.属性注入.方法注入,所谓注入相当赋值,下面一个一个来介绍 1:构造函数注入 1.1当类有多个构造函数时,可以通过InjectionConstructor特性 ...

  6. angular2 学习笔记 ( DI 依赖注入 )

    refer : http://blog.thoughtram.io/angular/2016/09/15/angular-2-final-is-out.html ( search Dependency ...

  7. Angular4.0从入门到实战打造在线竞拍网站学习笔记之三--依赖注入

    Angular4.0基础知识之组件 Angular4.0基础知识之路由 依赖注入(Dependency Injection) 正常情况下,我们写的代码应该是这样子的: let product = ne ...

  8. Asp.net core 学习笔记 ( DI 依赖注入 )

    比起 Angular 的依赖注入, core 的相对简单许多, 容易明白 所有 provider 都在 startup 里配置. public void ConfigureServices(IServ ...

  9. Spring.NET学习笔记1——控制反转(基础篇)

    在学习Spring.NET这个控制反转(IoC)和面向切面(AOP)的容器框架之前,我们先来看一下什么是控制反转(IoC). 控制反转(Inversion of Control,英文缩写为IoC),也 ...

随机推荐

  1. python入门-测试代码

    断言 测试函数 def get_formatted_name(first,last): """generate a neatly formattef full name& ...

  2. leetcode263

    public class Solution { private bool Judge(int x) { ) { return false; } int bound = Convert.ToInt32( ...

  3. JAVA时间进行比较和转换,时间加减得到天数

    转自:https://blog.csdn.net/iteye_8535/article/details/82246006 JAVA时间进行比较和转换,时间加减得到天数 1. 把时间类型的字符串转为DA ...

  4. JSON.Stringify()和JSON.parse()的比较使用

    1.  JSON.Stringify() 将一个对象解析成字符串 <script> function myonclick() { var value = $('select option: ...

  5. import 语句用于导入从外部模块,另一个脚本等导出的函数,对象或原语。

    import 语句用于导入从外部模块,另一个脚本等导出的函数,对象或原语. 注意:此功能目前无法在任何浏览器中实现.它在许多转换器中实现,例如 Traceur Compiler , Babel , R ...

  6. 安卓上为什么不能用system.io.file读取streammingAssets目录下的文件

    首先,看文档: Streaming Assets   Most assets in Unity are combined into the project when it is built. Howe ...

  7. 安装oracle后java -version命令显示 jdk version "1.3.1"的原因

    因为先装的JDK,后装了oracle,oracle的JDK配置把原来的jdk路径替换掉了. 我的电脑->属性->高级->环境变量->系统变量->PATH ,把JDK的路径 ...

  8. 使用CXF发布的WebService报错:org.apache.cxf.interceptor.Fault: The given SOAPAction does not match an operation

    场景:JAVA语言使用CXF搭建WebService发布报错 错误信息:org.apache.cxf.interceptor.Fault: The given SOAPAction does not ...

  9. JSP技术复习

    JSP是一种运行在服务器端的脚本语言,是用来开发动态网页的技术,它是Java Web程序的开发重要技术 JSP页面主要由HTML和JSP代码构成,JSP代码是通过"<%"和& ...

  10. js函数中变量声明提前

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...