关于IOC的概念就不多说了,在.NET平台下,比较优秀的IOC容器框架有如下四种,本文试图作一个简单的介绍,以及推荐一些各个框架的学习资源。
1.Castle
2.Spring.NET
3.ObjectBuilder
4.StructureMap
关于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("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推崇做法是使用配置文件来管理组件之间的依赖关系,当然它也支持自动装配,不过不推荐使用。这样使用配置文件的方式,带来的问题是当项目非常大的时候,配置文件会非常的繁琐,手工配置会变得很复杂,如下面的配置文件,需要指定每一个组件以及它们之间的依赖关系:
<?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>
官方主页: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))]

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);

}

}

}
官方主页: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(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)
{…}

}
项目主页:http://structuremap.sourceforge.net/Default.htm
学习资源:
现在只能参考官方文档了,还没有好的中文文档。
总结
以上简单介绍了.NET平台下四种不错的IOC容器框架,具体在项目中使用哪一个,就是仁者见仁,智者见智了,不过我个人仍然比较推崇Castle。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?