C# 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)

时间:2024-01-15 20:16:02

举例:在每天的日常生活中,我们离不开水,电,气。在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭。而城市化之后,人们从这些琐事中解放了出来,城市中出现了水厂,发电厂,燃气公司。水,电,气我们自己打开开关用就可以而不用关心这些都是怎么来的,怎么实现的。控制反转指的就是由我们自己提供水电气发展为由*提供水电气的整个过程。

可是在现实的编程世界中,我们只能怎么才能实现控制反转呢?这时依赖注入(DI: Independence Inject)出场了。依赖注入主要分为构造函数, 属性注入和依赖方法注入,注入。下面通过Unity框架对三种方式分别进行了实现:

电厂接口:IElectricity

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IOCTesting
{
interface IElectricity
{
string ProvideElectricity();
}
}

电厂类:ElectricityFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IOCTesting
{
class ElectricityFactory:IElectricity
{
public string ProvideElectricity()
{
return "The electricity is coming!";
}
}
}

水厂接口:IWater

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IOCTesting
{
interface IWater
{
string ProvideWater();
}
}

水厂类:WaterFactory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IOCTesting
{
class WaterFactory:IWater
{
public string ProvideWater()
{
return "The water is coming!";
}
}
}

人接口(人会用水,用电):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IOCTesting
{
public interface People
{
string UsingWater();
string UsingElectricity();
}
}

居民类(用构造函数,属性注入,依赖方法注入,三种方式分别对居民用水,用电方法进行的具体实现):

using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IOCTesting
{
#region 构造器注入
class ResidentsA:People
{
WaterFactory _wf;
ElectricityFactory _ef;
public ResidentsA(WaterFactory wf,ElectricityFactory ef)
{
this._ef = ef;
this._wf = wf;
}
public string UsingWater()
{
Console.WriteLine("I want to drink!");
return _wf.ProvideWater();
} public string UsingElectricity()
{
Console.WriteLine("I want to cook some food!");
return _ef.ProvideElectricity();
}
}
#endregion #region 属性注入
class ResidentsB : People
{
//属性注入
[Dependency]
public IWater _w { set; get; }
[Dependency]
public IElectricity _e { set; get; } public string UsingWater()
{
Console.WriteLine("I want to drink!");
return _w.ProvideWater();
} public string UsingElectricity()
{
Console.WriteLine("I want to cook some food!");
return _e.ProvideElectricity();
}
}
#endregion #region 依赖方法注入
class Residents : People
{
public IWater _iw;
public IElectricity _ie; public string UsingWater()
{
Console.WriteLine("I want to drink!");
return _iw.ProvideWater();
} public string UsingElectricity()
{
Console.WriteLine("I want to cook some food!");
return _ie.ProvideElectricity();
} [InjectionMethod]
public void UsingWaterTest(IWater iw)
{
Console.WriteLine("I want to drink!");
this._iw = iw;
} [InjectionMethod]
public void UsingElectricityTest(IElectricity ie)
{
Console.WriteLine("I want to cook some food!");
this._ie = ie;
}
}
#endregion
}

App.config配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
</configSections> <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
<!--register type="full class name,namespace"--> <register type="IOCTesting.IWater,IOCTesting" mapTo="IOCTesting.WaterFactory,IOCTesting">
<lifetime type="singleton"/>
</register> <register type="IOCTesting.IElectricity,IOCTesting" mapTo="IOCTesting.ElectricityFactory,IOCTesting">
<lifetime type="singleton"/>
</register> <!--<register type="IOCTesting.People,IOCTesting" mapTo="IOCTesting.ResidentsA,IOCTesting">
<lifetime type="singleton"/>
</register>-->
<!--<register type="IOCTesting.People,IOCTesting" mapTo="IOCTesting.ResidentsB,IOCTesting">
<lifetime type="singleton"/>
</register>-->
<register type="IOCTesting.People,IOCTesting" mapTo="IOCTesting.Residents,IOCTesting">
<lifetime type="singleton"/>
</register>
</container>
</unity>
</configuration>

具体的调用类Program.cs:

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IOCTesting
{
class Program
{
static void Main(string[] args)
{
//配置文件注册
IUnityContainer mycontanier = new UnityContainer();
UnityConfigurationSection section = (UnityConfigurationSection)System.Configuration.ConfigurationManager.GetSection("unity");
section.Configure(mycontanier); //创建调用者
//Console.WriteLine("-------------------Constructor Injection-------------------");
//ResidentsA pa = mycontanier.Resolve<People>() as ResidentsA;
//Console.WriteLine(pa.UsingWater());
//Console.WriteLine(pa.UsingElectricity()); //Console.WriteLine("-------------------Property Injection-------------------");
//ResidentsB pb = mycontanier.Resolve<People>() as ResidentsB;
//Console.WriteLine(pb.UsingWater());
//Console.WriteLine(pb.UsingElectricity()); Console.WriteLine("-------------------Method Injection-------------------");
Residents p = mycontanier.Resolve<People>() as Residents;
Console.WriteLine(p._iw.ProvideWater());
Console.WriteLine(p._ie.ProvideElectricity()); Console.ReadLine();
}
}
}

(备注:Unity框架需要提前自己安装,其实就是引用了两个包而已,在测试的过程中,如需测试构造函数注入,则需要在App.config和Program.cs中注释掉属性注入,依赖方法注入部分,否则会报错。)

ps. 1. 依赖方法注入会在被依赖对象创建时自动调用;

2. 由于被依赖对象是在程序运行时创建的所以这里面又用到了反射的技术,对于反射,我会继续创建反射的例子进行说明。