Unity - 如何对同一类型使用多个映射并注入到对象中

发布于 2024-10-27 17:30:40 字数 492 浏览 6 评论 0原文

我正在使用 Unity 2.0,在下面的代码中我尝试在 Worker 对象中注入特定的工具。

我想使用以下代码。但当然有一个错误“依赖关系解析失败”。我相信我应该能够做这样的事情,但我很难弄清楚。

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'm using Unity 2.0 and in the following code I'm trying to inject a specific tool in the Worker object.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶色山野 2024-11-03 17:30:40

有两种方法可以实现此目的:

您可以使用 ParameterOverride 和两步解析过程......

var tool = container.Resolve<ITool>("ToolB");
var worker = container.Resolve<IWorker>("Worker", 
    new ParameterOverride("tool", tool));

假设接收的 Worker 上的构造函数参数ITool 称为“工具”(多个 ParameterOverride 实例可以传递给 Resolve)。 IWorker 的命名实例具有的任何其他依赖项(通过构造函数或属性注入)也应该正确解析。

或者,为什么不设置需要指定 ITool 的命名 WorkerA、WorkerB、WorkerC 实例...

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")));

我认为后一种方法的缺点是,如果 Worker< /code> 需要额外的构造函数参数,您还需要将它们指定给 InjectionConstructor,指定顺序与您期望 Unity 使用的构造函数相同...

container.RegisterType<IWorker, Worker>("WorkerA", 
    new InjectionConstructor(typeof(SomeDependency), new ResolvedParameter<ITool>("ToolA"), typeof(SomeOtherDependency));

但是,Unity 会查找非上例中 SomeDependencySomeOtherDependency 的命名实例,这样可以节省您的一些工作。

There are two ways you can achieve this:

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));

...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.

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")));

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));

However, Unity will lookup the non-named instance of SomeDependency and SomeOtherDependency in the above example, so that saves you a bit of work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文