How can I register one singleton to different interfaces in unity, XML config?

社会主义新天地 提交于 2019-12-04 03:48:32

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:

IUnityContainer container = new UnityContainer().LoadConfiguration();

I didn't work with configuration files for unity yet, but according to the documentation it is

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="MyApp.Implementations" />
    <assembly name="MyApp" />
    <container>
        <register type="IEventService" mapTo="EventService" />
        <register type="IEventServiceInformation" mapTo="EventService" />
    </container>
</unity>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!