2011-10-21 17 views
8

Como hacerlo en el código se explica aquí: Unity Register two interfaces as one singleton¿Cómo puedo registrar un singleton en diferentes interfaces en unity, configuración XML?

_container.RegisterType<EventService>(new ContainerControlledLifetimeManager()); 
_container.RegisterType<IEventService, EventService>(); 
_container.RegisterType<IEventServiceInformation, EventService>(); 

bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>()); 

Como hacerlo en la configuración XML?

Respuesta

12

Personalmente me gusta explicar espacios de nombres y ensamblados en los alias. Así 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> 

código:

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

y están ambas instancias de Event_Class va a ser la misma? – lukebuehler

+2

Sí. Tus ReferenceEquals deberían funcionar. – ErnieL

0

yo no trabajo con los archivos de configuración de la unidad todavía, pero de acuerdo con la documentación que es

<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> 
+2

se ha perdido ErnieL

Cuestiones relacionadas