>>原來(lái)我是這樣解釋?xiě)?yīng)用IoC的意圖和方式的(但還是不夠清晰):如果我想" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

IoC and Castle.IoC

系統(tǒng) 2099 0

1. 什么是 IoC ?

關(guān)于IoC的詳細(xì)解釋請(qǐng)看 Inversion of Control Containers and the Dependency Injection pattern ( 英文 / 中文 ),本文只會(huì)作一些簡(jiǎn)單介紹

IoC(Inversion of Control),我們叫它"控制反轉(zhuǎn)",也可以叫它"依賴注入"(Dependency Injection)。

引用自 JGTM'2004 [MVP] 的話來(lái)解釋IoC
>>> 原來(lái)我是這樣解釋?xiě)?yīng)用IoC的意圖和方式的(但還是不夠清晰):如果我想打破A->B的依賴,那么我可以通過(guò)引入A、B之間的交互協(xié)議I來(lái)辦到,也 就是將A->B變?yōu)?A->I)+(I<-B)(此舉同時(shí)滿足了DIP和OCP原則),那么IoC就是幫助我們用各種各樣的方法(如構(gòu) 造器注入、屬性注入或接口注入等等)在運(yùn)行期把I的一個(gè)具體實(shí)現(xiàn)B傳達(dá)給A使用的一系列機(jī)制。

使用IoC的主要目地就是實(shí)現(xiàn)程序模塊的低偶合,在DotNet下比較著名的IoC框架是Castle和Spring.net。


2. IoC的三種形式

依賴注入的形式主要有三種,我分別將它們叫做構(gòu)造子注入(Constructor Injection)、設(shè)值方法注入(Setter Injection)和接口注入(Interface Injection)。


3. Castle中的IoC介紹

Castle.IoC 支持構(gòu)造子注入和設(shè)值方法注入,但更側(cè)重構(gòu)造子注入。


4. 使用Castle.IoC

使用 Castle.IoC 主要是配置 components

4.1 使用代碼配置

public static WindsorContainercontainer = new WindsorContainer( @" ../../config.xml " );
container.AddComponent(
" FormatService " , typeof (FormatService));
container.AddComponent(
" format " , typeof (IMessageFormat), typeof (HtmlMessage));

4.2 使用XML文件配置
<? xmlversion="1.0"encoding="utf-8" ?>
< castle >
< components >
< component id ="FormatService"
type
="IoCSample.FormatService,IoCSample" />

< component id ="format"
service
="IoCSample.Components.IMessageFormat,IoCSample"
type
="IoCSample.Components.HtmlMessage,IoCSample" />
</ components >
</ castle >


如何配置IList,IDictionary,Array等復(fù)雜類型

IDictionary
代碼:
public class C
{
public C() {}
public C(IDictionaryd)
{
this .dictionary = d;
}

private IDictionarydictionary;
public IDictionaryDictionary
{
get { return this .dictionary;}
}

}
配置:
< component id ="c" type ="CastleDemo.C,CastleDemo" >
< parameters >
< d >
< item keyType ="System.String" valueType ="System.String" >
< item key ="a" > a </ item >
< item key ="b" > b </ item >
</ item >
</ d >
</ parameters >
</ component >


IList
代碼:
public class D
{
private IListlist;
public D() {}
public IListList
{
get { return this .list;}
}

public D(IListlist)
{
this .list = list;
}

}
配置:
< component id ="d" type ="CastleDemo.D,CastleDemo" >
< parameters >
< list >
< item type ="System.String" >
< item > a </ item >
< item > b </ item >
< item > c </ item >
</ item >
</ list >
</ parameters >
</ component >


Array
代碼:
public class E
{
private int []ages;
public int []Ages
{
get { return this .ages;}
}

public E()
{

}

public E( int []ages)
{
this .ages = ages;
}

}
配置:
< component id ="e" type ="CastleDemo.E,CastleDemo" >
< parameters >
< ages >
< item type ="System.Int32" >
< item > 1 </ item >
< item > 2 </ item >
< item > 3 </ item >
</ item >
</ ages >
</ parameters >
</ component >

IoC and Castle.IoC


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 新平| 固原市| 黄骅市| 封丘县| 饶阳县| 建湖县| 漳平市| 娄烦县| 铜鼓县| 乐亭县| 城固县| 应城市| 平乡县| 封开县| 常山县| 洛隆县| 吉安市| 咸丰县| 深圳市| 山阳县| 东至县| 炎陵县| 贵州省| 绥化市| 滨州市| 旬邑县| 于田县| 乌兰察布市| 松桃| 和田市| 北流市| 宁津县| 赤壁市| 杭州市| 军事| 栾城县| 疏附县| 惠安县| 海林市| 威信县| 老河口市|