Castle IOC容器與Spring.NET配置之比較

來源:互聯網
上載者:User

我本人對於Spring.NET並不瞭解,本文只是通過一個簡單的例子來比較一下兩者配置之間的區別。在Castle IOC容器中,提出了自動裝配(Auto-Wiring)的概念,即由容器自動管理組件之間的依賴關係,我們無需自己編寫XML設定檔來配置組件之間的依賴關係。在Spring.NET中也是支援自動裝配的,但是並不推薦使用,它貫穿著一種思想就是一切皆為XML配置,這是兩者之間最大的一個區別。

關於自動裝配,來自於Spring.NET的支援者認為讓容器自動管理,會讓我們無法控制組件的依賴關係,如果該為XML配置,可以讓我們知道自己在做什麼,我們指定了哪些依賴關係,方便進行控制和管理;而來自於Castle IOC的支援者認為如果不讓容器自動管理,手工配置會變得非常之複雜,設定檔也會變得非常繁冗,如果系統中的組件非常之多的時候,管理工作會變得很困難。

我們來看一個簡單的例子,有這樣一個組件MyMainComponent,它依賴於MyComponent1、MyComponent2,並且它在建構函式中還需要接收一個整型的參數。

//出處:http://terrylee.cnblogs.com

public class MyMainComponent
{
    MyComponent1 _com1;

    MyComponent2 _com2;

    int _i;

    public MyMainComponent(MyComponent1 com1,MyComponent2 com2,int i)
    {
        this._com1 = com1;

        this._com2 = com2;

        this._i = i;
    }
}

public class MyComponent1
{
    public MyComponent1()
    {
        //
    }
}

public class MyComponent2
{
    public MyComponent2()
    {
        //
    }
}

如果用採用Spring.NET,它採用XML進行組件之間的串連,設定檔如下,需要在設定檔中指定每一個對象及其它們之間的依賴,同時在設定檔中區分是建構函式還是其他方法:

<!--出處:http://terrylee.cnblogs.com-->

<?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>

Castle IOC中同樣需要設定檔,但相比之下,就簡單了很多:

<!--出處:http://terrylee.cnblogs.com-->

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

    <components>

        <component id="myMainComponent">

            <parameters>

                <i>1</i>

            </parameters>

        </component>

    </components>

</configuration>

在Castle IOC中的配置並不需要指定組件之間的關聯,它會自動通過Windsor來處理;我們只是配置了一個參數i,這個i是MyMainComponent中的建構函式中不存在依賴關係的那個參數。

//出處:http://terrylee.cnblogs.com

public class App
{
    public static void Main()
    {
        IWindsorContainer container = new WindsorContainer(new XmlInterpreter("../../BasicUsage.xml") );

        container.AddComponent( "myMainComponent", 

            typeof(MyMainComponent));

        container.AddComponent( "myComponent1", 

            typeof(MyComponent1));

        container.AddComponent( "myComponent2", 

            typeof(MyComponent2));          

    }
}

這樣添加組件後,

WindsorContainer會自動調用MicroKernel中的ConstructorDependenciesModelInspector來處理組件的建構函式依賴。

通過上面的這個簡單例子比較可以看出,如果我們想要增加一個組件之間的依賴關係或者增加一個組件使用Castle要比使用Spring.NET容易很多,Spring.NET複雜的設定檔會給我們開發帶來很來不可預料的錯誤;Castle根據對象的依賴關係,採用自動裝配,不需要配置組件的依賴,另外為了符合構造注入和屬性注入,Castle的設定檔並沒有像Spring.Net那樣區分建構函式還是其他的方法,同時直接使用Parameters,而不是使用建構函式參數之類的區分。

 

參考資料

Castle的官方網站http://www.castleproject.org

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.