Unity vs Castle Windsor

refer:http://richardsbraindump.blogspot.com/2008/05/unity-vs-castle-windsor.html

Recently I did a post on breaking dependencies which used the Castle Windsor container as the Inversion of Control container.  I thought I'd also have a look at doing the same thing with the Unity container from Microsoft's Patterns & Practices team, which was version 1.0'ed last month.

The good news?  It's pretty much just some slight syntax changes and that's about it.  The only problem I found was that after installing the Unity help file for VS2008 my Visual Studio help is now broken and now I have to go and repair my Visual Studio installation.  I'd suggest sticking to the CHM file instead.

Here's the only differences between the two applications:

1. References (duh!)
Castle Windsor
using Castle.Windsor;
using Castle.Core.Resource;
using Castle.Windsor.Configuration.Interpreters;

Unity
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

2. Instantiating the Container

Castle Windsor
            IWindsorContainer container = new WindsorContainer(new XmlInterpreter());

Unity
            IUnityContainer container = new UnityContainer();

UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);

3. The .config files

Castle Windsor
<?xml version="1.0" encoding="utf-8" ?>
<
configuration>
<
configSections>
<
section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</
configSections>

<
castle>
<
components>
<
component id="prompt.input" service="SalesPrompter.IInputControl, SalesPrompter"
type="SalesPrompter.PromptInputControl, SalesPrompter" />
<
component id="console.display" service="SalesTax.IMessageDisplay, SalesTax"
type="SalesTax.ConsoleMessageDisplay, SalesTax" />
<
component id="input.parser" service="SalesTax.IInputParser, SalesTax"
type="SalesTax.InputParser, SalesTax" />
<
component id="salelinefactory" service="SalesTax.ISaleLineFactory, SalesTax"
type="SalesTax.SaleLineFactory, SalesTax" />
</
components>
</
castle>

</
configuration>

Unity
<?xml version="1.0" encoding="utf-8" ?>
<
configuration>
<
configSections>
<
section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</
configSections>

<
unity>
<
typeAliases></typeAliases>
<
containers>
<
container>
<
types>
<
type type="SalesPrompter.IInputControl, SalesPrompter"
mapTo="SalesPrompter.PromptInputControl, SalesPrompter"/>
<
type type="SalesTax.IMessageDisplay, SalesTax"
mapTo="SalesTax.ConsoleMessageDisplay, SalesTax"/>
<
type type="SalesTax.IInputParser, SalesTax"
mapTo="SalesTax.InputParser, SalesTax"/>
<
type type="SalesTax.ISaleLineFactory, SalesTax"
mapTo="SalesTax.SaleLineFactory, SalesTax"/>
</
types>
</
container>
</
containers>
</
unity>

</
configuration>

 


Note that there was nothing different in the resolving of references.  Both containers have a Resolve<T>() method that does exactly the same thing.


So, how easy is that? :-)  The P&P team have done a pretty good job with this container.  Admittedly I haven't really looked into some of the more advanced options, but it looks quite good and being from the P&P team it's likely to get more traction that the Castle Windsor container in the corporate development world.

 

转载于:https://www.cnblogs.com/try/articles/1208752.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值