【本段摘录自:IOC容器Unity 使用 http://blog.csdn.net/gdjlc/article/details/8695266】
面向接口实现有很多好处,可以提供不同灵活的子类实现,增加代码稳定和健壮性等,但是接口一定是需要实现的,如果一个子类实现换成另一个子类实现,就需要在代码中改动,或者建立一个工厂来根据条件生成,还是存着着一定的耦合关系。
依赖注入(Dependency Injection,DI),也叫控制反转(Inversion of
Control,IoC)是一个重要的面向对象编程的法则用来削减程序的耦合问题,它把耦合从代码中移出去,放到统一的XML文件中,通过一个容器在需要的时候把这个依赖关系形成,即把需要的接口实现注入到需要它的类中,当需要换一个实现子类将会变成很简单(一般这样的对象都是实现于某种接口的),只要修改XML就可以,这样可以实现对象的热插拨(有点象USB接口)。
Unity 是一个轻量级、可扩展的依赖注入容器,支持构造函数、属性和方法的依赖注入。
1.C# Unity使用(独立配置文件unity.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 configSource="unity.config" />
</configuration>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <!--alias 定義別名--> <alias alias="IClass" type="ConsoleApplication1.IClass, ConsoleApplication1" /> <alias alias="MyClass" type="ConsoleApplication1.MyClass, ConsoleApplication1" /> <!--引入下面的命名空間,可以省的輸入過多的內容--> <namespace name="ConsoleApplication1" /> <assembly name="ConsoleApplication1" /> <container > <register type="IClass" mapTo="MyClass" /> <register type="ILogger" mapTo="FileLogger"></register> <!--註冊類+構造函數 默認數值是3--> <register type="InstallerBase" mapTo="BlogInstaller"> <constructor> <param name="pubContext" type="IPubContext" /> <param name="sum" type="System.Int32" value="3" /> </constructor> </register> <!--<register type="ISomeInterface[]" mapTo="MyTypeImplementingSomeInterface[]"/>--> <register type="ISomeInterface[System.Int32]" mapTo="MyTypeImplementingSomeInterface[System.Int32]"/> <register type="ObjectWithOverloads" name="callFirstOverload"> <method name="CallMe"> <param name="param" type="int" value="17"/> </method> </register> <register type="ILogger" mapTo="MockLogger" name="validLogger" /> <register type="ObjectUsingLogger" name="dependencyRegistered"> <property name="Logger"> <optional name="validLogger" /> </property> </register> <!--數值--> <register type="ILogger" name="main" mapTo="MockLogger" /> <register type="ILogger" name="another" mapTo="MockLogger" /> <register type="ILogger" name="special" mapTo="SpecialLogger" /> <register type="ArrayDependencyObject" name="specificElements"> <property name="Loggers"> <array> <dependency name="main" /> <dependency name="another" /> </array> </property> </register> </container> </unity>
2. Using Unity for IoC and DI(codeproject国外网站给出的如何将unity.config作为web.config或者app.config的独立配置文件来引入)
<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity configSource="unity.config"/>
此处给出unity.config文件的具体代码:
<?xml version="1.0" encoding="utf-8"?> <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <typeAliases> <!-- Models--> <typeAlias alias="IBook" type="BusinessBackend.IBook, BusinessBackend" /> <typeAlias alias="Book" type="BusinessBackend.Book, BusinessBackend" /> <!-- Services --> <typeAlias alias="IBookService" type="BusinessBackend.IBookService, BusinessBackend" /> <typeAlias alias="BookService" type="BusinessBackend.BookService, BusinessBackend" /> <!-- Repositories --> <typeAlias alias="IBookRepository" type="BusinessBackend.IBookRepository, BusinessBackend" /> <typeAlias alias="BookRepository" type="BusinessBackend.BookRepository, BusinessBackend" /> </typeAliases> <container> <register type="IBook" mapTo="Book" /> <register type="IBookRepository" mapTo="BookRepository" name="SQLrepo" /> <register type="IBookService" mapTo="BookService" > <constructor> <param name="br" dependencyName="SQLrepo"> <!--<param name="br" dependencyType="BookRepository">--> <!--<dependency type="BookRepository" />--> <!--<dependency name="SQLrepo" />--> </param> </constructor> </register> </container> </unity>
3.Unity 2.0 Web.config settings with MVC(使用unity的答疑帖子)
http://stackoverflow.com/questions/3925019/unity-2-0-web-config-settings-with-mvc