分享程序员开发的那些事...
首页
NTFS
漏洞
Android
Magento
服务器
Python
powershell
技术问答
Linux
网站首页
且构网 - 分享程序员编程开发的那些事
您所在的位置:
首页
>
Castle IOC容器与Spring.NET配置之比较
Castle IOC容器与Spring.NET配置之比较
更新时间:2022-09-21 18:09:54
我本人对于
Spring.NET
并不了解,本文只是通过一个简单的例子来比较一下两者配置之间的区别。在
Castle IOC
容器中,提出了自动装配(
Auto-Wiring
)的概念,即由容器自动管理组件之间的依赖关系,我们无需自己编写
XML
配置文件来配置组件之间的依赖关系。在
Spring.NET
中也是支持自动装配的,但是并不推荐使用,它贯穿着一种思想就是一切皆为
XML
配置,这是两者之间最大的一个区别。
关于自动装配,来自于
Spring.NET
的支持者认为让容器自动管理,会让我们无法控制组件的依赖关系,如果该为
XML
配置,可以让我们知道自己在做什么,我们指定了哪些依赖关系,方便进行控制和管理;而来自于
Castle IOC
的支持者认为如果不让容器自动管理,手工配置会变得非常之复杂,配置文件也会变得非常繁冗,如果系统中的组件非常之多的时候,管理工作会变得很困难。
我们来看一个简单的例子,有这样一个组件
MyMainComponent
,它依赖于
MyComponent1
、
MyComponent2
,并且它在构造函数中还需要接收一个整型的参数。
//
出处:
[url]http://terrylee.cnblogs.com[/url]
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
进行组件之间的连接,配置文件如下,需要在配置文件中指定每一个对象及其它们之间的依赖,同时在配置文件中区分是构造函数还是其他方法:
<!--
出处:[url]http://terrylee.cnblogs.com[/url]
-->
<?
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
中同样需要配置文件,但相比之下,就简单了很多:
<!--
出处:[url]http://terrylee.cnblogs.com[/url]
-->
<?
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
中的构造函数中不存在依赖关系的那个参数。
//
出处:
[url]http://terrylee.cnblogs.com[/url]
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
的官方网站
[url]http://www.castleproject.org[/url]
本文转自lihuijun51CTO博客,原文链接:
http://blog.51cto.com/terrylee/67681
,如需转载请自行联系原作者
上一篇 : :用好VS2010扩展管理器
下一篇 : iscsi
相关阅读
Castle IOC容器与Spring.NET配置之比较
推荐文章
云存储标准应运而生
更换云服务供应商的征兆
使用VMware主机客户端管理单台ESXi主机
如何避免虚拟基础设施单点故障
初创企业不上云拿什么“火拼”?
EPS应急电源的应用常见问题合集
如何重新定义云数据中心的资源利用率
react+typescript实践总结
智能建筑中的机房空调与新风系统
找到云植入模式的***时机