2012-06-04 53 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。 –

相关问题