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. linux运维工程师工作中的一些常见问题解决方法

    http://blog.sina.com.cn/s/blog_b9fe247a0101anoe.html 1.shell脚本死活不执行 问题:某天研发某同事找我说帮他看看他写的shell脚本,死活不执 ...

  2. HiveThrift

    Hive具有一个可选的组件叫HiveServer或HiveThrift,其允许通过指定端口访问Hive.Thrift是一种软件架构,用于跨语言的服务开发. hive最常用的访问方式是采用cli访问,不 ...

  3. Jsch - java SFTP 文件上传下载

    使用Jsch上传.下载文件,核心步骤是:获取channel,然后使用get/put方法下载.上传文件 核心代码句: session = jSch.getSession(ftpUserName, ftp ...

  4. Delphi 解析HTML

    uses mshtml; IHTMLEleMent.ID; IHTMLEleMent.tagName; IHTMLEleMent.title;elmt._className;elmt.getAttri ...

  5. 序列化和反序列化(json 和pickle)dumps 为序列化, json为反序列化

    json 可以在不同语言中进行使用 下面先介绍一下json的适用方法 import json, pickle t1 = { 'name':'alex', ', ' } t1 = json.dumps( ...

  6. Spring MVC 异常处理 - DefaultHandlerExceptionResolver

    对一些特殊的异常进行处理,比如方法类型不匹配, 转换错误.

  7. starling 第一天

    flashplayer_27_sa_debug: https://files.cnblogs.com/files/dt1991/flashplayer_27_sa_debug.rar flashpla ...

  8. C#--反射技术

    反射:反射为了动态(比如动态的加载dll,动态创建类型.动态调用方法等) 引用 using System.Reflection 原理: 一个类库编译后会生成一个以.dll结尾的文件,一个以.pdb结尾 ...

  9. 在linux中运行main方法所在的java类(亲测有效!!!)

    本人是用SecureCRTPortable连接linux终端的.其他工具连接linux终端应该是一样的操作! 一.首先到移动到java工程所在的bin目录. 二.在bin目录下执行javac -cp ...

  10. Xtrabackup安装及使用

    官方安装步骤:https://www.percona.com/doc/percona-xtrabackup/2.4/installation/yum_repo.html 安装percona repo源 ...