关于IOC的概念就不多说了,在.NET平台下,比较优秀的IOC容器框架有如下四种,本文试图作一个简单的介绍,以及推荐一些各个框架的学习资源。
一.Castle
在Castle中包含了一组开发框架,它里面的IOC容器是Windsor,目前Castle已经发布了RC1版本,其中Windsor已经是RC3了。在Windsor中提出了自动装配的概念,由容器来自动管理组件之间的依赖关系,无需用户去编写XML配置文件或者通过Attribute来指定容器之间的依赖关系。这样在使用上非常的简单,同时也带了一些问题,作为开发人员的我们无法控制组件的依赖关系。如下面的XML配置文件,仅仅是设定了组件的参数而已:
<?
xml version="1.0" encoding="utf-8"
?>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
configuration
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
components
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
component
id
="myMainComponent"
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
parameters
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
i
>
1
</
i
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
parameters
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
component
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
components
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
configuration
>
简单的使用:
public
class
App
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public static void Main()
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
IWindsorContainer container = new WindsorContainer(new XmlInterpreter("http://www.cnblogs.com/BasicUsage.xml"));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
container.AddComponent("myMainComponent",
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
typeof(MyMainComponent));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
container.AddComponent("myComponent1",
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
typeof(MyComponent1));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
container.AddComponent("myComponent2",
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
typeof(MyComponent2));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
官方主页: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推崇做法是使用配置文件来管理组件之间的依赖关系,当然它也支持自动装配,不过不推荐使用。这样使用配置文件的方式,带来的问题是当项目非常大的时候,配置文件会非常的繁琐,手工配置会变得很复杂,如下面的配置文件,需要指定每一个组件以及它们之间的依赖关系:
<?
xml version="1.0" encoding="utf-8"
?>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
configuration
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
object
id
="myManComponent"
class
="CastleDemo.MyMainComponent, CastleDemo"
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
constructor-arg
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
ref
object
="mycomponent1"
/>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
constructor-arg
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
constructor-arg
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
ref
object
="mycomponent2"
/>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
constructor-arg
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
constructor-arg
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
value
>
1
</
value
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
constructor-arg
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
object
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
object
id
="mycomponent1"
class
="CastleDemo.MyComponent1, CastleDemo"
/>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
object
id
="mycomponent2"
class
="CastleDemo.MyComponent2, CastleDemo"
/>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</
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
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private IEmailSender _sender;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private ITemplateEngine _templateEngine;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public SimpleNewsletterService(
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Dependency(CreateType = typeof(SmtpEmailSender))]
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IEmailSender sender,
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Dependency(CreateType = typeof(NVelocityTemplateEngine))]
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ITemplateEngine templateEngine)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_sender = sender;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_templateEngine = templateEngine;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Dispatch(String from, String[] targets, String message)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String msg = _templateEngine.Process(message);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (String target in targets)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_sender.Send(from, target, msg);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
官方主页: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
"
)]
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
SqlDataSource : IDataSource
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private readonly string _sql;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private readonly IDatabase _database;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public SqlDataSource(IDatabase database, string sql)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_sql = sql;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_database = database;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public DataTable FetchTable()
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return _database.FetchDataTable(_sql);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[Pluggable(
"
Email
"
)]
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
EmailAction : IAction
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public EmailAction(string to, string body)
{…}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void Process(DataTable table)
{…}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[Pluggable(
"
Daily
"
)]
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
DailyScheduler : IScheduler
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DailyScheduler()
{}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime GetNextRunTime(DateTime currentTime)
{…}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
项目主页:http://structuremap.sourceforge.net/Default.htm
学习资源:
现在只能参考官方文档了,还没有好的中文文档。
总结
以上简单介绍了.NET平台下四种不错的IOC容器框架,具体在项目中使用哪一个,就是仁者见仁,智者见智了,不过我个人仍然比较推崇Castle。