我怎样才能注册一个单以团结不同的接口,XML配置? [英] How can I register one singleton to different interfaces in unity, XML config?
本文介绍了我怎样才能注册一个单以团结不同的接口,XML配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何做到这一点在code在此说明: <一href="http://stackoverflow.com/questions/1394650/unity-register-two-interfaces-as-one-singleton">Unity注册两个接口为一体的单
How to do it in code is explained here: Unity Register two interfaces as one singleton
_container.RegisterType<EventService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IEventService, EventService>();
_container.RegisterType<IEventServiceInformation, EventService>();
bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>());
如何做到这一点的XML配置?
How to do it in the XML config?
推荐答案
我个人喜欢拼出命名空间和程序集的别名。所以XML:
Personally I like to spell out namespaces and assemblies in aliases. So xml:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="Event_Interface" type="Mynamespace.IEventService, MyAssembly"/>
<alias alias="EventService_Interface" type="Mynamespace.IEventServiceInformation, MyAssembly"/>
<alias alias="Event_Class" type="Mynamespace.EventService, MyAssembly"/>
<container>
<register type="Event_Interface" mapTo="Event_Class">
<lifetime type="singleton"/>
</register>
<register type="EventService_Interface" mapTo="Event_Class">
<lifetime type="singleton"/>
</register>
</container>
</unity>
code:
code:
IUnityContainer container = new UnityContainer().LoadConfiguration();
这篇关于我怎样才能注册一个单以团结不同的接口,XML配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文