關於IOC的概念就很少說了,在.NET平臺下,比較優秀的IOC容器框架有以下四種,本文試圖做一個簡單的介紹,以及推薦一些各個框架的學習資源。
一.Castle
在Castle中包含了一組開發框架,它裏面的IOC容器是Windsor,目前Castle已經發布了RC1版本,其中Windsor已是RC3了。在Windsor中提出了自動裝配的概念,由容器來自動管理組件之間的依賴關係,無需用戶去編寫XML配置文件或者經過Attribute來指定容器之間的依賴關係。這樣在使用上很是的簡單,同時也帶了一些問題,做爲開發人員的咱們沒法控制組件的依賴關係。以下面的XML配置文件,僅僅是設定了組件的參數而已:
<?
xml version="1.0" encoding="utf-8"
?>

<
configuration
>

<
components
>

<
component
id
="myMainComponent"
>

<
parameters
>

<
i
>
1
</
i
>

</
parameters
>

</
component
>

</
components
>

</
configuration
>
簡單的使用:
public
class
App


{
public static void Main()


{
IWindsorContainer container = new WindsorContainer(new XmlInterpreter("[url]http://www.cnblogs.com/BasicUsage.xml[/url]"));

container.AddComponent("myMainComponent",

typeof(MyMainComponent));

container.AddComponent("myComponent1",

typeof(MyComponent1));

container.AddComponent("myComponent2",

typeof(MyComponent2));

}

}
學習資源:
TerryLee
的Castle系列:
二.Spring.NET
Spring.NET
是從java的Spring Framework移植過來的,如今版本應該是Spring.NET 1.0.2。正好和前面說的Castle相反,Spring.NET推崇作法是使用配置文件來管理組件之間的依賴關係,固然它也支持自動裝配,不過不推薦使用。這樣使用配置文件的方式,帶來的問題是當項目很是大的時候,配置文件會很是的繁瑣,手工配置會變得很複雜,以下面的配置文件,須要指定每個組件以及它們之間的依賴關係:
<?
xml version="1.0" encoding="utf-8"
?>

<
configuration
>

<
object
id
="myManComponent"
class
="CastleDemo.MyMainComponent, CastleDemo"
>

<
constructor-arg
>

<
ref
object
="mycomponent1"
/>

</
constructor-arg
>

<
constructor-arg
>

<
ref
object
="mycomponent2"
/>

</
constructor-arg
>

<
constructor-arg
>

<
value
>
1
</
value
>

</
constructor-arg
>

</
object
>

<
object
id
="mycomponent1"
class
="CastleDemo.MyComponent1, CastleDemo"
/>

<
object
id
="mycomponent2"
class
="CastleDemo.MyComponent2, CastleDemo"
/>

</
configuration
>
學習資源:
三.ObjectBuilder
ObjectBuilder
,只看其名字就知道是用來構造對象的,是由微軟模式與實踐小組最先開發並使用在CAB,由於表現出色,後來在Enterprise Library中也使用它來負責對象的建立工做,由於OB能夠說是微軟的IOC容器,它也是一個輕量級的IOC框架。它與前面介紹的Spring.NET不少類似的地方,須要顯式的經過Attribute來指定對象之間的依賴關係,以下面來自於idior給出的代碼片段:
public
class
SimpleNewsletterService : INewsletterService


{
private IEmailSender _sender;

private ITemplateEngine _templateEngine;

public SimpleNewsletterService(

[Dependency(CreateType = typeof(SmtpEmailSender))]

IEmailSender sender,

[Dependency(CreateType = typeof(NVelocityTemplateEngine))]

ITemplateEngine templateEngine)


{

_sender = sender;

_templateEngine = templateEngine;

}

public void Dispatch(String from, String[] targets, String message)


{

String msg = _templateEngine.Process(message);

foreach (String target in targets)


{

_sender.Send(from, target, msg);

}

}

}
學習資源:
四.StructureMap
前面介紹的三個你們可能都比較熟悉了,這最後一個估計關注的人就比較少了。StructureMap也是.NET環境下的一個輕量級依賴注入工具,StructureMap是一個靈活的、可擴展的通用「插件」機制的.NET IOC框架,支持.NET1.1和2.0。它與Spring.NET比較相似,可是它只支持使用Attribute的方式,而不能經過XML文件來配置,這樣雖然顯得不夠靈活,可是它避免了項目比較大時XML文件的繁瑣問題。以下面代碼片段所示:
[Pluggable(
"
SQL
"
)]

public
class
SqlDataSource : IDataSource


{
private readonly string _sql;

private readonly IDatabase _database;

public SqlDataSource(IDatabase database, string sql)


{
_sql = sql;

_database = database;
}

public DataTable FetchTable()


{

return _database.FetchDataTable(_sql);

}

}

[Pluggable(
"
Email
"
)]

public
class
EmailAction : IAction


{


public EmailAction(string to, string body)
{…}


public void Process(DataTable table)
{…}

}

[Pluggable(
"
Daily
"
)]

public
class
DailyScheduler : IScheduler


{

public DailyScheduler()
{}


public DateTime GetNextRunTime(DateTime currentTime)
{…}

}
學習資源:
如今只能參考官方文檔了,尚未好的中文文檔。
總結
以上簡單介紹了.NET平臺下四種不錯的IOC容器框架,具體在項目中使用哪個,就是仁者見仁,智者見智了,不過我我的仍然比較推崇Castle。