zoukankan      html  css  js  c++  java
  • Castle与Spring.Net在用法和配置上的区别

    用法:

    Castle

    Castle通过使用Windsor容器,或者直接使用MicroKernel来管理应用中对象的连接:

    IWindsorContainer container = new WindsorContainer();
    container.AddComponent( 
    "key1"typeof(IMyService), typeof(ServiceImpl) );
    container.AddComponent( 
    "key2"typeof(MyMailService) );

    Spring.Net

    Spring.Net的世界以IObjectFactory为中心。你可以自己实现IObjectFactory也可以使用已经实现了的。但是,无论怎样,这一切都是围绕着XML来实现的。例如,你可能存在下面的代码,目的是从XML中获取应用中的对象的连接:

    Stream input = new FileStream ("objects.xml", FileMode.Open); 
    XmlObjectFactory factory 
    = new XmlObjectFactory(input);

    对象连接处理:

    Castle认为使用XML配置文件配置对象连接的信息过于复杂和繁琐,因此Castle使用自动连接。但是,尽管Castle使用自动连接,并非表明Castle完全抛弃了XML配置文件。Castle把主要注意力放在对象连接的处理上,而XML配置文件则配置非对象的连接。例如下面的一个例子,Spring.NET和Castle的处理方式上大不相同:

    例:

    public class ExampleObject



    private AnotherObject myObjectOne;

    private YetAnotherObject myObjectTwo;

    private int i;

    public ExampleObject(AnotherObject anotherObject, YetAnotherObject yetAnotherObject, int i)



    myObjectOne 
    = anotherObject;

    myObjectTwo 
    = yetAnotherObject;

    this.i = i;

    }

    }



    Spring.Net的处理:

    Sprint.Net用下面的XML配置文件来达到对象的连接。

    <object id="exampleObject" class="Examples.ExampleObject, ExamplesLibrary"> 

        
    <constructor-arg> 

            
    <ref object="anotherExampleObject"/> 

        
    </constructor-arg> 

        
    <constructor-arg> 

            
    <ref object="yetAnotherObject"/> 

        
    </constructor-arg> 

        
    <constructor-arg>

            
    <value>1</value>

        
    </constructor-arg> 

    </object> 

    <object id="anotherExampleObject" class="Examples.AnotherObject, ExamplesLibrary"/> 

    <object id="yetAnotherObject" class="Examples.YetAnotherObject, ExamplesLibrary"/> 

    Castle的处理:

    前面说了,Castle并没有放弃XML的配置文件,因此Castle的配置文件如下:

    <configuration> 
        
    <components> 
            
    <component id="exampleObject"> 
                
    <parameters> 
                    
    <i>1</i> 
                
    </parameters> 
            
    </component> 
        
    </components> 
    </configuration>


    Castle的配置文件中并没有说明对象的连接,那么Castle如何处理呢?
    Castle通过Windsor处理:

    WindsorContainer container = new WindsorContainer( new XmlConfigurationStore("myfile.xml") ); 
    container.AddComponent( 
    "exampleObject"typeof(ExampleObject) ); 
       container.AddComponent( 
    "anotherExampleObject"typeof(AnotherObject) ); 
       container.AddComponent( 
    "yetAnotherObject"typeof(YetAnotherObject) );

    比较:
    Spring.Net需要在配置配置文件中详细的描述每一个对象对其他对象的依赖。而Castle则是通过Windsor容器自动处理对象之间的依赖关系。

    那么Castle需要配置文件作什么呢?

    上面我们可以看到Castle用配置文件配置了一个参数i,这个 i 恰好就是ExampleObject的构造函数的一个不存在对象依赖关系的参数的名称,所以,如果构造函数的参数不是i,而是其它的,例如smtpPort,那么参数的Tag名称也就是smtpPort。

    从上面的例子可以看出,Castle认为Spring.Net的XML配置文件过于复杂,在对象依赖关系复杂,对象多,开发人员人数多的情况下,XML配置文件会带来不必要的麻烦和不可预料的错误。因此,Castle根据对象的依赖关系,自动连接对象,不需要XML的描述,所以,为了符合构造注入和属性注入,Castle的配置文件没有像Spring.Net那样区分构造函数还是其他的方法,同时,直接使用Parameters,而不是使用构造函数参数之类的区分。 

    文章来源:http://spaces.msn.com/members/channel/Blog/cns!1pBSOH5RGy_uirisRz3VdnXQ!128.entry
  • 相关阅读:
    公有云、私有云与混合云到底有什么区别?
    分享两个“整人”的脚本语言代码
    最强搜书工具集合
    关于云服务器恢复快照时后服务器变得卡顿等问题的解决方法
    Centos8.0编译安装稳定最新版的nginx
    彻底禁止Win10自动更新工具Windows Update Blocker v1.5 汉化版
    阿里云轻量应用服务器回滚快照后造成卡顿问题的解决方法
    IaaS、PaaS和SaaS的区别
    Everything文件搜索工具
    Windows10自带软件一款性能监控工具
  • 原文地址:https://www.cnblogs.com/iaxes/p/101478.html
Copyright © 2011-2022 走看看