如何注册实现同一接口的多种类型 [英] How to register multiple types that implement the same interface
本文介绍了如何注册实现同一接口的多种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只有一个接口,并且有2个类正在使用它。我正在使用统一配置来基于接口标识实例。
I have a single interface and this is being used by 2 classes. I am using unity configuration to identify the instance based on the interface.
现在,我想知道我应该如何注册这些类型,以便可以基于以下内容调用适当的实现:
Now I want to know how should i register these types so that i can call the appropriate implementation based on the single interface itself.
推荐答案
这就是我的操作方式:
var container = new UnityContainer().RegisterType<IAmImplementedMoreThanOnce, Implementation1>("Implementation1")
.RegisterType<IAmImplementedMoreThanOnce, Implementation2>("Implementation2")
.RegisterType<IHaveDependencies1, WithDependenciesImplementation1>(new InjectionConstructor(new ResolvedParameter<IAmImplementedMoreThanOnce>("Implementation1")))
.RegisterType<IHaveDependencies2, WithDependenciesImplementation2>(new InjectionConstructor(new ResolvedParameter<IAmImplementedMoreThanOnce>("Implementation2")));
这篇关于如何注册实现同一接口的多种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文