注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 IB客座主编(四)美国西蒙公..
 帮助

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


2006-12-01 08:51:00
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://terrylee.blog.51cto.com/342737/67590
关于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://wj.cnblogs.com/[中文]
TerryLeeCastle系列:
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>
学习资源:

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

        }


    }


}
学习资源:
NiwalkerObjectBuilder技术内幕:http://blog.csdn.net/niwalker/category/18174.aspx[中文]
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){…}

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

本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67590





    文章评论
 
 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: