Castle IOC容器与Spring.NET配置之比较

我本人对于Spring.NET并不了解,本文只是通过一个简单的例子来比较一下两者配置之间的区别。在Castle IOC容器中,提出了自动装配(Auto-Wiring)的概念,即由容器自动管理组件之间的依赖关系,我们无需自己编写XML配置文件来配置组件之间的依赖关系。在Spring.NET中也是支持自动装配的,但是并不推荐使用,它贯穿着一种思想就是一切皆为XML配置,这是两者之间最大的一个区别。

关于自动装配,来自于Spring.NET的支持者认为让容器自动管理,会让我们无法控制组件的依赖关系,如果该为XML配置,可以让我们知道自己在做什么,我们指定了哪些依赖关系,方便进行控制和管理;而来自于Castle IOC的支持者认为如果不让容器自动管理,手工配置会变得非常之复杂,配置文件也会变得非常繁冗,如果系统中的组件非常之多的时候,管理工作会变得很困难。

我们来看一个简单的例子,有这样一个组件MyMainComponent,它依赖于MyComponent1MyComponent2,并且它在构造函数中还需要接收一个整型的参数。

//出处: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,这个iMyMainComponent中的构造函数中不存在依赖关系的那个参数。

//出处: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

posted @ 2006-04-25 08:35  TerryLee  阅读(26607)  评论(17编辑  收藏  举报