Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>());

How to do it in the XML config?

like image 827
lukebuehler Avatar asked Oct 21 '11 00:10

lukebuehler


1 Answers

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();
like image 88
ErnieL Avatar answered Oct 16 '22 19:10

ErnieL