2011-10-21 8 views
8

Come fare in codice è spiegato qui: Unity Register two interfaces as one singletonCome posso registrare un singleton su diverse interfacce in unità, XML config?

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

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

Come fare nella configurazione XML?

risposta

12

Personalmente mi piace precisare spazi dei nomi e le assemblee in alias. Così 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> 

codice:

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

e sono entrambe le istanze di event_class andando essere lo stesso? – lukebuehler

+2

Sì. I tuoi ReferenceEquals dovrebbero funzionare. – ErnieL

0

non ho lavorato con i file di configurazione per l'unità ancora, ma in base alla documentazione che è

<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

vi siete persi ErnieL

Problemi correlati