.NET開源項目介紹及資源推薦:IOC容器篇

作者: Terrylee  來源: 博客園  發布時間: 2008-08-17 00:01  閱讀: 3631 次  推薦: 0   原文鏈接   [收藏]  

關于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/[中文]

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.springframework.net/

學習資源:

官方文檔:http://www.springframework.net/documentation.html[英文]

雨痕的幾篇文章:http://www.rainsts.net/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.sourceforge.net/Default.htm

學習資源:

現在只能參考官方文檔了,還沒有好的中文文檔。

 

總結

以上簡單介紹了.NET平臺下四種不錯的IOC容器框架,具體在項目中使用哪一個,就是仁者見仁,智者見智了,不過我個人仍然比較推崇Castle

作者:TerryLee
出處:http://terrylee.cnblogs.com
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
0
0
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()