关于IOC 的概念就不多说了,在.NET 平台下,比较优秀的IOC 容器框架有如下四种,本文试图作一个简单的介绍,以及推荐一些各个框架的学习资源。
一.Castle
在Castle 中包含了一组开发框架,它里面的IOC 容器 是Windsor ,目前Castle 已经发 布了RC1 版本,其中Windsor 已经是RC3 了。在Windsor 中提出了自动装配的概念, 由容器来自动管理组件之间的依赖关系,无需用户去编写XML 配置文件或者通过Attribute 来指定容器之间的依赖关系。这样在使用上非常的简单,同时也带了一些问题,作为开发人员的我 们无法控制组件的依赖关系。如下面的XML 配置文件,仅仅是设定了组件的参数而已:
<?
xmlversion="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()


{
IWindsorContainercontainer = new WindsorContainer( new XmlInterpreter( " http://www.cnblogs.com/BasicUsage.xml " ));

container.AddComponent( " myMainComponent " ,

typeof (MyMainComponent));

container.AddComponent( " myComponent1 " ,

typeof (MyComponent1));

container.AddComponent( " myComponent2 " ,

typeof (MyComponent2));

}

}
官方主页:http://www.castleproject.org/
学习资源:
官方文档:http://www.castleproject.org/container/documentation/v1rc3/index.html
叶子的家:http://wj.cnblogs.com/ [ 中 文]
TerryLee 的Castle 系列:
http://terrylee.cnblogs.com/archive/2006/04/28/castl_ioc_article.html [ 中文]
Ayende 一篇非常棒的文章:http://msdn2.microsoft.com/en-us/library/aa973811.aspx [ 英 文]
二.Spring.NET
Spring.NET 是从java 的Spring Framework 移植过来的,现在版本应该是Spring.NET 1.0.2 。正好和前面说的Castle 相反,Spring.NET 推崇做法是使用配置文件来管理组件之间的依赖关系,当然它也支持自动装配,不过不推荐使 用。这样使用配置文件的方式,带来的问题是当项目非常大的时候,配置文件会非常的繁琐,手工配置会变得很复杂,如下面的配置文件,需要指定每一个组件以及 它们之间的依赖关系:
<?
xmlversion="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
>
官方主页:http://www.springframework.net/
学习资源:
官方文档:http://www.springframework.net/documentation.html [ 英 文]
雨痕的几篇文章:http://www.rainsts.net/default.asp?cat=13
Zhuzl 的Spring.NET 系列:http://blog.csdn.net/zlz_212/category/241716.aspx
三.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))]

IEmailSendersender,

[Dependency(CreateType = typeof (NVelocityTemplateEngine))]

ITemplateEnginetemplateEngine)


{

_sender = sender;

_templateEngine = templateEngine;

}

public void Dispatch(Stringfrom,String[]targets,Stringmessage)


{

Stringmsg = _templateEngine.Process(message);

foreach (Stringtarget in targets)


{

_sender.Send(from,target,msg);

}

}

}
官方主页:http://msdn.microsoft.com/practices/
学习资源:
Niwalker 的ObjectBuilder 技术内幕:http://blog.csdn.net/niwalker/category/18174.aspx [ 中 文]
浪子学编程系列:http://www.cnblogs.com/walkingboy/category/54596.html [ 中 文]
Idior 的EnterLib ObjectBuild vs Castle WindsorContainer :http://www.cnblogs.com/idior/archive/2006/08/15/ObjectBuildvsCastle.html [ 中 文]
四.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(IDatabasedatabase, string sql)


{
_sql = sql;

_database = database;
}

public DataTableFetchTable()


{

return _database.FetchDataTable(_sql);

}

}

[Pluggable(
"
Email
"
)]

public
class
EmailAction:IAction


{


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


public void Process(DataTabletable)
{…}

}

[Pluggable(
"
Daily
"
)]

public
class
DailyScheduler:IScheduler


{

public DailyScheduler()
{}


public DateTimeGetNextRunTime(DateTimecurrentTime)
{…}

}
项目主页:http://structuremap.sourceforge.net/Default.htm
学习资源:
现在只能参考官方文档 了,还没有好的中文文档。
总结
以上简单介绍了.NET 平台下四种不错的IOC 容器框架,具体在项目 中使用哪一个,就是仁者见仁,智者见智了,不过我个人仍然比较推崇Castle 。