2012-06-04 88 views
1

假設我有一個名爲IPumaServices的接口,並且我有兩個實現它的類:POSiXmlServices和TaXmlServices。如何選擇在Microsoft Unity中註冊的類型的實現

現在我有另一個名爲IPumaNotification的接口,實現它的類被稱爲PumaNotification。 PumaNotification的構造函數接收IPumaServices實現。

我的問題: 在Unity中,我該如何註冊PumaNotification的實現,它在構造器中注入POSiXmlServices,另一個注入TaXmlServices?

這是我到目前爲止。

using (_unityContainer = new UnityContainer()) 
      { 
       _unityContainer 
       .RegisterType<IPumaServices, POSiXmlServices>("POSiXml") 
       .RegisterType<IPumaServices, TaXmlServices>("TaXml") 
       .RegisterType<IPumaNotification, PumaNotification>(); 
      } 

我不知道如何使它與我上面的要求一起工作。

我無法在線研究此問題,因爲我不確定如何描述我所面臨的問題。

我很感激任何幫助。

回答

4

可以指定解析參數到構造函數,從而解決您希望實例:

using (_unityContainer = new UnityContainer()) 
     { 
      _unityContainer 
      .RegisterType<IPumaServices, POSiXmlServices>("POSiXml") 
      .RegisterType<IPumaServices, TaXmlServices>("TaXml") 
      .RegisterType<IPumaNotification, PumaNotification>(
       new InjectionConstructor(      // Explicitly specify a constructor 
       new ResolvedParameter<IPumaServices>("TaXml") // Resolve parameter of type 
      ); 
     } 

如果要註冊兩個IPumaServices,可以適當命名每一個和解決那些名字,當你使用它們。

+0

正是我所需要的,thnx。 –

相關問題