2013-07-10 38 views
0

同一個接口的兩個對象我有,我需要實例化同一接口的兩個不同的實現,都在同一個類中的情況。Instatiate與統一在同一個班級

public AutoMapperRegisterFactory(IRegisterAutoMapper registerAutoMapper , IRegisterAutoMapper registerAutoMapperMobile) 
{ 
    m_RegisterAutoMapper = registerAutoMapper; 
} 

我怎麼會去告訴團結第一IRegisterAutoMapper應該是RegisterAutoMapper類型和RegisterAutoMapperMobile類型中的第二個嗎?

回答

1

您可以使用IRegisterAutoMapper的多個命名映射並結合使用一個InjectionConstructor告訴Unity每個參數使用哪些特定的映射。

IUnityContainer container = new UnityContainer() 
    .RegisterType<IRegisterAutoMapper, RegisterAutoMapper>() //default 
    .RegisterType<IRegisterAutoMapper, MobileRegisterAutoMapper>("Mobile") 
    .RegisterType<AutoMapperRegisterFactory>(
     new InjectionConstructor(
      typeof(IRegisterAutoMapper), 
      new ResolvedParameter<IRegisterAutoMapper>("Mobile"))); 
相關問題