spring.net 概念-控制反转(又名依赖注入)

Spring .net 作为一个应用程序框架,在构建企业级.net应用程序提供了很多灵活而又丰富的功能(如:依赖注入,aop,数据访问抽象,asp.net 扩展)。

控制反转:

Inversion of Control:简称IoC :是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI),还有一种方式叫“依赖查找”(Dependency Lookup)。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体,将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。

http://q.cnblogs.com/q/33437/ 解释

举个例子,组件A中有类ClassA,组件B中有接口IB和其对应的实现类B1和B2。

那么,现在ClassA需要利用IB接口来做一些事情,例如:

public class ClassA {
    public void DoSomething() {
        IB b = ???
 b.DoWork();
    }
} 

现在的问题来了,IB b = ??? 中这三个???要写什么代码?是要写成 IB b = new B1(),还是要写成IB b = new B2() ?

不管是哪一种,都会让ClassA强依赖于IB的实现。

在上面这种方案中,ClassA通过new一个B1或B2来实现对IB的依赖的获取,换句话说,ClassA在主动获取依赖。

这样的设计会让ClassA很难扩展,那我们要改良设计:使用依赖注入。上面说到了,问题出在new这里,也就是依赖是Class去主动获取的,那我们就要解决这个问题:不要去主动获取对IB的依赖(通过new),而让这个依赖从ClassA的外面“注入”进来。注入有多种方式,比较常用的一种是通过构造函数注入,那么,我们要把ClassA改成:

public class ClassA {
    private IB b;

    public ClassA(IB b) {
        this.b = b;
    }

    public DoSomething() {
        this.b.DoWork();
    }
}

可以看到,通过把IB这个依赖从构造函数中“注”进来后,ClassA就不依赖IB的实现了。还可以发现,这个重构过程中,我们是把"ClassA主动获取对IB的依赖”变成“把对IB的依赖从外部注入到ClassA中”,依赖的方向反转了,所以,依赖注入又称“控制反转”。

 

另一种

参考 http://www.cnblogs.com/yahue/p/3508501.html

有一只宠物:

public interface Pet
    {
        string name { get; set; }
        void bark();
    }

 

小狗:

public class Dog : Pet
{
        public string name { get; set; }
        public void bark()
        {
            Console.WriteLine("汪汪汪汪汪汪汪汪汪...");
        }
}

 

 

人: 

 public class Person
{
    public string name { get; set; }
    public Pet pet { get; set; }
}

 

 

一个简单的spring框架:

项目引用:Spring.core --整个框架的基础,实现了依赖注入的功能

              Spring.AOP--提供面向方面编程(aop)的支持

      Spring.Data--a定义了一个抽象的数据访问层,可以跨越各种数据访问技术(从ADO.NET到各种orm)进行数据访问。

          项目配置文件:app.config

  

复制代码
<?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="file://objects.xml"></resource>
    </context>
    <objects  xmlns="http://www.springframework.net">
     
    </objects>
  </spring>
</configuration>
复制代码

 

  objects.xml  属性为始终复制,不然上面配置的<resource uri="file://objects.xml"></resource>找不到。  

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.net
        http://www.springframework.net/xsd/spring-objects.xsd">
  <object id="person" type="SpringDemo.Person,SpringDemo" singleton="true" >
    <property name="pet" ref="dog" ></property>
    <property name="name" value="Yahue"></property>
  </object>
  <object id="dog" type="SpringDemo.Dog,SpringDemo"  singleton="true">
    <property name="name" value="旺财"></property>
  </object>
</objects>
复制代码

 

  控制台程序中:

复制代码
static void fistIoC()
{

Person p = ctx.GetObject("person") as Person; Console.WriteLine(p.pet.name);
Console.WriteLine("-------------------------");
}
复制代码

 

  调用:

复制代码
static IApplicationContext ctx = ContextRegistry.GetContext();
        static void Main(string[] args)
        {
            fistIoC();
            Console.ReadLine();
        } 
复制代码

 

控制台输出:

旺财

 
posted @ 2016-07-11 10:05  开发许  阅读(214)  评论(0编辑  收藏  举报