2011-10-21 10 views
8

कोड में यह करने के लिए कैसे यहाँ समझाया गया है: 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>()); 

कैसे एक्सएमएल config में यह करने के लिए?

उत्तर

12

व्यक्तिगत तौर पर मैं उपनाम में नामस्थान और विधानसभाओं उल्लेख करना चाहते है। तो 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> 

कोड:

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

याद किया और Event_Class वाला के दोनों मामलों में ही हो रहे हैं? – lukebuehler

+2

हां। आपके संदर्भ एक्वाल्स काम करना चाहिए। – ErnieL

0

मैं अभी तक एकता के लिए विन्यास फाइल के साथ काम नहीं किया है, लेकिन दस्तावेज़ के अनुसार यह

<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

आप <जीवनकाल प्रकार = "सिंगलटन" /> – ErnieL

संबंधित मुद्दे

 संबंधित मुद्दे