简体   繁体   English

使用ninject将多个实现绑定到同一个接口

[英]Bind multiple implementations to the same interface with ninject

Why is it not possible for me to do the following in Ninect? 为什么我不能在Ninect中执行以下操作?

Kernel.Bind<IPresenter>.To<DefaultPresenter>();
Kernel.Bind<IPresenter>.To<DashboardPresenter>();
Kernel.Bind<IPresenter>.To<HeartRatePresenter>();
Kernel.Bind<IPresenter>.To<GPSPresenter>();

Each of the 4 implementations have a different constructor that expect a different type. 4个实现中的每一个都有不同的构造函数,期望不同的类型。 When i attempt this, Ninject throws an exception telling me that i cannot bind to the same interface more than once. 当我尝试这个时,Ninject会抛出一个异常,告诉我我不能多次绑定到同一个界面。

In a class called Presentable which all presenter classes inherit from, I am attempting to do Kernel.Get<IPresenter>(new ConstructorArgument("view", this)) so assign IPresentable Presenter within the page/view where the page/view implements an interface that the presenter expects as a parameter. 在所有演示者类继承的名为Presentable类中,我尝试执行Kernel.Get<IPresenter>(new ConstructorArgument("view", this))因此在页面/视图中分配IPresentable Presenter ,其中页面/视图实现了演示者期望作为参数的接口。

What is a way around this so that ninject recognises different constructor parameter types? 有什么方法可以让ninject识别不同的构造函数参数类型?

The binding to multiple interfaces is fine. 绑定到多个接口很好。 Ninject allows this. Ninject允许这样做。 see here: https://github.com/ninject/Ninject/wiki/Multi-injection 请看这里: https//github.com/ninject/Ninject/wiki/Multi-injection

The problem is that Ninject can not just magically give you the "one" that you want depending on constructor arguments. 问题是Ninject不能只是根据构造函数参数神奇地给你你想要的“一个”。 What Ninject is designed to do with the code you wrote is to give you ALL of the bindings at once, when you ask for a List. Ninject旨在处理您编写的代码是为了在您请求List时立即为您提供所有绑定。

So like others said, if you only want a single instance, it sounds like what you want is contextual bindings. 所以像其他人说的那样,如果你只想要一个实例,听起来你想要的是上下文绑定。 However, the way you asked your question and the other answers are a bit confusing, because it makes it sound like multi-injection is not possible, but it is possible, if it is really what you want. 然而,你问你的问题和其他答案的方式有点混乱,因为它听起来像多次注射是不可能的,但它是可能的,如果它真的是你想要的。 (which in this case it isn't) (在这种情况下不是)

You need to tell ninject how it should know which binding to choose. 你需要告诉ninject它应该知道选择哪个绑定。

Have a look at conditional binding: https://github.com/ninject/ninject/wiki/Contextual-Binding 看看条件绑定: https//github.com/ninject/ninject/wiki/Contextual-Binding

I would recommend using the .When(...) syntax but maybe you prefer using .Named(...) (iE assigning a name/identifier to every binding and passing that name to ninject in the .Get<IPresenter>("SomeName") call). 我建议使用.Named(...) .When(...)语法,但是你可能更喜欢使用.Named(...) (iE为每个绑定分配一个名称/标识符,并将该名称传递给.Get<IPresenter>("SomeName")中的.Get<IPresenter>("SomeName")调用)。

You need to use contextual bindings. 您需要使用上下文绑定。 I usually use the concrete "target" class to decide which service implementation the kernel will provide by using .WhenInjectedInto() or .WhenInjectecExactlyInto() . 我通常使用具体的“目标”类来决定内核将使用.WhenInjectedInto().WhenInjectecExactlyInto()来提供哪些服务实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM