Ninject 2.0 - 结合到使用相同的接口一次以上对象? [英] Ninject 2.0 - binding to a object that uses the same interface more than once?
问题描述
考虑以下几点:
公共的东西(IInterface concreteObjectOne,IInterface concreteObjectTwo)
{
this.concreteObjectOne = concreteObjectOne;
this.concreteObjectTwo = concreteObjectTwo;
}
我如何设置设置此类型与Ninject结合起来?我想尝试谷歌搜索这个词,但我不知道这是什么叫我不能,我也不能找到这对任何维基
修改
我相信这称为基于公约绑定,所描述的<一个href=\"http://ninject.$c$cplex.com/wikipage?title=Conventions-Based%20Binding&referringTitle=User%20Guide\">here.然而,这种文档是1.0和2.0版本不具有仅
方法。我想这是没有实现的属性 - 使用名称或类似的东西的约定
在除了使用仅的方法的,该文章建议通过指定所注入的对象的不同属性另一种解决方案。
示例:
公共类ObjectOneAttribute:属性
{}
公共类ObjectTwoAttribute:属性
{}
然后
公共的东西([ObjectOneAttribute] IInterface concreteObjectOne,[ObjectTwoAttribute] IInterface concreteObjectTwo)
{
this.concreteObjectOne = concreteObjectOne;
this.concreteObjectTwo = concreteObjectTwo;
}
而当你想将接口绑定到正确的具体对象,请使用WhereTargetHas的方法:
<$p$p><$c$c>Bind<IInterface>().To<YourConcreteTypeOne>().WhereTargetHas<ObjectOneAttribute>();绑定&所述; IInterface方式&gt;()到&lt; YourConcreteTypeTwo&GT;()WhereTargetHas&所述。ObjectTwoAttribute&GT;();
更新:解决方案,而无需使用属性:结果
使用的方法当
绑定&LT; IInterface&GT;()为&lt;。YourConcreteTypeOne方式&gt;()当(R = GT; r.Target.Name ==concreteObjectOne);
绑定&所述; IInterface方式&gt;()到&lt; YourConcreteTypeTwo方式&gt;()时(R =&GT; r.Target.Name ==concreteObjectTwo)
Consider the following:
public Something(IInterface concreteObjectOne, IInterface concreteObjectTwo)
{
this.concreteObjectOne = concreteObjectOne;
this.concreteObjectTwo = concreteObjectTwo;
}
How do I set set this type of binding up with Ninject? I'd try Googling the term, but as I'm not sure what this is called I can't, nor can I find anything on the Wiki about this.
Edit:
I believe this is called Convention Based Binding, as described here. However this documentation is for version 1.0 and 2.0 does not have the Only
method. I'd like this to be achieved without attributes - using the convention of names or something similiar.
In addition to the use of "Only" method, the article suggests another solution by specifying different attributes for the injected objects.
Example:
public class ObjectOneAttribute : Attribute
{
}
public class ObjectTwoAttribute : Attribute
{
}
Then
public Something([ObjectOneAttribute] IInterface concreteObjectOne, [ObjectTwoAttribute] IInterface concreteObjectTwo)
{
this.concreteObjectOne = concreteObjectOne;
this.concreteObjectTwo = concreteObjectTwo;
}
And when you want to bind the interface to the correct concrete object, use the "WhereTargetHas" method:
Bind<IInterface>().To<YourConcreteTypeOne>().WhereTargetHas<ObjectOneAttribute>();
Bind<IInterface>().To<YourConcreteTypeTwo>().WhereTargetHas<ObjectTwoAttribute>();
Update: Solution without using attributes:
Use the method "When":
Bind<IInterface>().To<YourConcreteTypeOne>().When(r => r.Target.Name == "concreteObjectOne");
Bind<IInterface>().To<YourConcreteTypeTwo>().When(r => r.Target.Name == "concreteObjectTwo")
;
这篇关于Ninject 2.0 - 结合到使用相同的接口一次以上对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!