统一 - 如何使用多个映射为相同的类型,并注入到一个目的 [英] Unity - how to use multiple mappings for the same type and inject into an object
问题描述
我使用Unity 2.0,并在下面的代码,我试图注入了Worker对象的特定工具。
I'm using Unity 2.0 and in the following code I'm trying to inject a specific tool in the Worker object.
我想用下面的代码。但ofcourse有错误失败的依赖议案。我相信我应该可以做这样的事,但我有困难的时候计算出来。
I would like to use the following code. But ofcourse there is an error "Resolution of the dependency failed". I believe I should be able to do something like this, but I'm having a difficult time figuring it out.
IUnityContainer container = new UnityContainer();
container.RegisterType<IWorker, Worker>("Worker")
.RegisterType<ITool, ToolA>("ToolA")
.RegisterType<ITool, ToolB>("ToolB")
.RegisterType<ITool, ToolC>("ToolC");
IWorker worker = container.Resolve<Worker>("ToolA");
我知道这并不工作,但我会如何解决这个问题?
I know this doesn't work, but how would I resolve this issue?
BarDev
推荐答案
有两种方法可以实现这一点:
There are two ways you can achieve this:
您可以使用 ParameterOverride
的和两个步骤的解决过程...
You can use ParameterOverride
's and a two step resolution process...
var tool = container.Resolve<ITool>("ToolB");
var worker = container.Resolve<IWorker>("Worker",
new ParameterOverride("tool", tool));
...假设在工人构造函数的参数
接收 itool用于
被称为工具(多个 ParameterOverride
实例可以传递给解析
)。任何其他依赖(通过构造函数或属性注入)的命名实例 IWorker
已应正确解决为好。
...assuming that the constructor argument on Worker
that receives the ITool
is called 'tool' (multiple ParameterOverride
instances can be passed to Resolve
). Any other dependencies (via constructor or property injection) that the named instance of IWorker
has should be correctly resolved as well.
另外,为什么不设置一个名为 WorkerA,WorkerB,WorkerC
需要指定实例 itool用于
...
Alternatively, why not setup named WorkerA, WorkerB, WorkerC
instances that require the specified ITool
...
container.RegisterType<ITool, ToolA>("ToolA");
container.RegisterType<ITool, ToolB>("ToolB");
container.RegisterType<ITool, ToolC>("ToolC");
container.RegisterType<IWorker, Worker>("WorkerA",
new InjectionConstructor(new ResolvedParameter<ITool>("ToolA")));
container.RegisterType<IWorker, Worker>("WorkerB",
new InjectionConstructor(new ResolvedParameter<ITool>("ToolB")));
container.RegisterType<IWorker, Worker>("WorkerC",
new InjectionConstructor(new ResolvedParameter<ITool>("ToolC")));
缺点我用后一种方法假设是,如果工人
带你需要将它们指定额外的构造函数的参数 InjectionConstructor
为好,因为构造你期望统一使用相同的顺序指定...
The disadvantage I suppose with the latter approach is that if Worker
takes additional constructor parameters you will need to specify them to the InjectionConstructor
as well, specified in the same order as the constructor you are expecting Unity to use...
container.RegisterType<IWorker, Worker>("WorkerA",
new InjectionConstructor(typeof(SomeDependency), new ResolvedParameter<ITool>("ToolA"), typeof(SomeOtherDependency));
不过,统一将查找的非命名实例 SomeDependency
和 SomeOtherDependency
在上面的例子中,这样可以节省一些工作。
However, Unity will lookup the non-named instance of SomeDependency
and SomeOtherDependency
in the above example, so that saves you a bit of work.
这篇关于统一 - 如何使用多个映射为相同的类型,并注入到一个目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!