1
我有這個簡化的問題構造。我有多個命名實例,您可以看到IBusinessLogic的註冊數量隨着每個添加的參數而增加。有沒有優雅的方式來實現下面的功能,而不需要爲IBusinessLogic提供如此多的RegisterType。統一DI,多個命名實例
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.RegisterType<ISettings, Settings1>("settings1");
container.RegisterType<ISettings, Settings2>("settings2");
container.RegisterType<IRepository, Repository1>("repository1 settings1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1")));
container.RegisterType<IRepository, Repository1>("repository1 settings2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2")));
container.RegisterType<IRepository, Repository1>("repository2 settings1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1")));
container.RegisterType<IRepository, Repository1>("repository2 settings2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2")));
container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1 settings1 repository1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1"), new ResolvedParameter<IRepository>("repository1 settings1")));
container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1 settings2 repository1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2"), new ResolvedParameter<IRepository>("repository1 settings1")));
container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1 settings1 repository2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1"), new ResolvedParameter<IRepository>("repository2 settings2")));
container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1 settings2 repository2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2"), new ResolvedParameter<IRepository>("repository2 settings2")));
container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2 settings1 repository1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1"), new ResolvedParameter<IRepository>("repository1 settings1")));
container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2 settings2 repository1", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2"), new ResolvedParameter<IRepository>("repository1 settings1")));
container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2 settings1 repository2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings1"), new ResolvedParameter<IRepository>("repository2 settings2")));
container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2 settings2 repository2", new InjectionConstructor(new ResolvedParameter<ISettings>("settings2"), new ResolvedParameter<IRepository>("repository2 settings2")));
string inputParam1 = "logic1";
string inputParam2 = "settings2";
string inputParam3 = "repository1";
var instance = container.Resolve<IBusinessLogic>(inputParam1 + " " + inputParam2 + " " + inputParam3);
}
}
編輯:
我找到了另一種方式,但我有依賴關係圖中明確指定每個覆蓋
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.RegisterType<ISettings, Settings1>("settings1");
container.RegisterType<ISettings, Settings2>("settings2");
container.RegisterType<IRepository, Repository1>("repository1");
container.RegisterType<IRepository, Repository2>("repository2");
container.RegisterType<IBusinessLogic, BusinessLogic1>("logic1");
container.RegisterType<IBusinessLogic, BusinessLogic2>("logic2");
string inputParam1 = "logic1";
string inputParam2 = "settings1";
string inputParam3 = "repository1";
var instance = container.Resolve<IBusinessLogic>(inputParam1, new DependencyOverrides()
{
{typeof(ISettings),container.Resolve<ISettings>(inputParam2)},
{typeof(IRepository),container.Resolve<IRepository>(inputParam3, new DependencyOverrides()
{
{typeof(ISettings),container.Resolve<ISettings>(inputParam2)}
})
},
});
}
}
我真正需要的是告訴容器 - 解決IBusinessLogic和使用在需要ISettings實例的依賴關係圖中,「settings1」命名實例無處不在。那可能嗎?
你能解釋一下這部分*「解決IBusinessLogic和使用‘’你需要ISettings實例依賴圖中處處命名實例」設置1 *?如果你只需要「settings1」,爲什麼要註冊「settings2」?另外,你能解釋你的代碼中哪些客戶端(類)需要哪些依賴關係嗎? –
你的代碼中是否有'BusinessLogic2'?你似乎只有'BusinessLogic1'。 –
我修改了代碼並添加了BusinessLogic2 – ekalchev