分享

.NET开源项目介绍及资源推荐:IOC容器篇

 贾朋亮博客 2014-12-04

关于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./

学习资源:

官方文档:http://www./container/documentation/v1rc3/index.html

叶子的家:http://wj.cnblogs.com/[中文]

TerryLeeCastle系列:

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是从javaSpring 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./

学习资源:

官方文档:http://www./documentation.html[英文]

雨痕的几篇文章:http://www./default.asp?cat=13

ZhuzlSpring.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/

学习资源:

NiwalkerObjectBuilder技术内幕:http://blog.csdn.net/niwalker/category/18174.aspx[中文]

浪子学编程系列:http://www.cnblogs.com/walkingboy/category/54596.html[中文]

IdiorEnterLib ObjectBuild vs Castle WindsorContainerhttp://www.cnblogs.com/idior/archive/2006/08/15/ObjectBuildvsCastle.html[中文]


四.StructureMap

前面介绍的三个大家可能都比较熟悉了,这最后一个估计关注的人就比较少了。StructureMap也是.NET环境下的一个轻量级依赖注入工具,StructureMap是一个灵活的、可扩展的通用“插件”机制的.NET IOC框架,支持.NET1.12.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./Default.htm

学习资源:

现在只能参考官方文档了,还没有好的中文文档。

总结

以上简单介绍了.NET平台下四种不错的IOC容器框架,具体在项目中使用哪一个,就是仁者见仁,智者见智了,不过我个人仍然比较推崇Castle

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多