C# 基于method属性将接口绑定到ninject中的两个不同的具体对象

C# 基于method属性将接口绑定到ninject中的两个不同的具体对象,c#,inversion-of-control,ninject,C#,Inversion Of Control,Ninject,我有两个不同的具体对象,让我们保存ConcreteOne和ConcreteTwo。每一个都实现了一个接口ILooseyGoosey。我希望ninject根据该方法的属性调用不同的方法 这就是我到目前为止所做的: public class ConcreteOne : ILooseyGoosey { public void SomeMethod() { }; } public class ConcreteTwo : ILooseyGoosey { public void SomeMethod

我有两个不同的具体对象,让我们保存ConcreteOne和ConcreteTwo。每一个都实现了一个接口ILooseyGoosey。我希望ninject根据该方法的属性调用不同的方法

这就是我到目前为止所做的:

public class ConcreteOne : ILooseyGoosey
{
  public void SomeMethod() { };
}
public class ConcreteTwo : ILooseyGoosey
{
  public void SomeMethod() { } ;
}
public interface ILooseyGoosey
{
  [CallConcreteTwo()]
  void SomeMethod();
}
这是我在Ninject模块中定义的:

public override void Load()
{
  Bind<ILooseyGoosey>().To<ConcreteOne>().InjectMethodsWhere(mi => mi.GetCustomAttributes(true).Where(a => a.GetType() == typeof(CallConcreteTwoAttribute)).Count() == 0);
  Bind<ILooseyGoosey>().To<ConcreteTwo>().InjectMethodsWhere(mi => mi.GetCustomAttributes(true).Where(a => a.GetType() == typeof(CallConcreteTwoAttribute)).Count() > 0);
}
public override void Load()
{
Bind().To().InjectMethodsWhere(mi=>mi.GetCustomAttributes(true)。其中(a=>a.GetType()==typeof(CallConcreteTwoAttribute)).Count()==0);
Bind().To().InjectMethodsWhere(mi=>mi.GetCustomAttributes(true)。其中(a=>a.GetType()==typeof(CallConcreteTwoAttribute)).Count()>0);
}
我得到的错误是:

System.NotSupportedException:注册服务ILooseyGoosey时出错:为服务声明了多个默认绑定。 找到2个默认绑定:


问题是,您将一个接口分配给两个实现,而没有任何条件逻辑。您正在应用的逻辑仅应用于注入的方法。Ninject不知道要使用哪个绑定,因为您指出它们都是默认的。

不确定您是否仍然需要答案,基于元数据的方法是一种方法。您可能希望在Ninject 2.0中以元数据的方式进行绑定。请参阅不是我写的。请参阅:

尼尼特 下载it扩展贡献访问Dojo Speak up赞助商商品 版本: »Ninject »MVC3 多注入编辑页面历史记录 Ninject允许您注入绑定到特定类型或接口的多个对象。例如,如果我们有IWeapon接口和两个实现,剑和匕首: 公共接口IWeapon { 弦击(弦靶); } 公共级剑:IWeapon { 公共字符串命中(字符串目标) { 返回“切片”+目标+“对半”; } } 公共级匕首:IWeapon { 公共字符串命中(字符串目标) { 返回“刺+目标+死亡”; } } 我们有武士班。您可以看到它的构造函数接受一个IWeapon数组。 公营武士 { 只读IEnumerable所有武器; 公共武士(IWeapon[]所有武器) { 这个。所有武器=所有武器; } 公共无效攻击(字符串目标) { foreach(此中为IWeapon武器。所有武器) 控制台。写线(武器。命中(目标)); } } 我们可以创建从IWeapon接口到剑和匕首类型的绑定。 类TestModule:Ninject.Modules.NinjectModule { 公共覆盖无效负载() { 绑定()到(); 绑定()到(); } } 最后,我们使用上面定义的模块创建了一个内核。我们向Ninject要一个武士的例子。现在,当你要求武士攻击时,你会看到它得到了一个绑定到IWeapon的所有类型的数组。 班级计划 { 公共静态void Main() { Ninject.IKernel kernel=新的标准内核(新的TestModule()); var samurai=kernel.Get(); 武士。攻击(“你的敌人”); } } 你会看到: 刺死你的敌人 把你的敌人切成两半 内核还公开了一个GetAll方法,该方法允许您通过执行以下操作生成相同的输出: 班级计划 { 公共静态void Main() { Ninject.IKernel kernel=新的标准内核(新的TestModule()); IEnumerable=kernel.GetAll(); foreach(武器中的var武器) Console.WriteLine(武器打击(“作恶者”); } } 继续阅读:对象范围 恩卡里 Ninject是Nate Kohari的私生子。版权所有©2007-2012 Enkari有限公司和Ninject项目贡献者。
伊恩-谢谢你的回复。我知道问题是什么,但我不知道如何解决它。你有什么想法吗?我如何指定这两种方法中的哪一种,而不是全部?例如,如果我只想使用匕首呢?请查看工厂模式:这样工厂将找到正确的实例。 Ninject Download it Extensions Contribute Visit the Dojo Speak up Sponsors Merchandise Version: »Ninject »MVC3 Multi injectionEdit PagePage History Ninject allows you to inject multiple objects bound to a particular type or interface. For example, if we have our IWeapon interface, and two implementations, Sword and Dagger: public interface IWeapon { string Hit(string target); } public class Sword : IWeapon { public string Hit(string target) { return "Slice " + target + " in half"; } } public class Dagger : IWeapon { public string Hit(string target) { return "Stab " + target + " to death"; } } Here we have the Samurai class. You can see that its constructor takes an array of IWeapon. public class Samurai { readonly IEnumerable allWeapons; public Samurai(IWeapon[] allWeapons) { this.allWeapons = allWeapons; } public void Attack(string target) { foreach (IWeapon weapon in this.allWeapons) Console.WriteLine(weapon.Hit(target)); } } We can create bindings from the IWeapon interface to the Sword and Dagger types. class TestModule : Ninject.Modules.NinjectModule { public override void Load() { Bind().To(); Bind().To(); } } Finally, a kernel is created with the module we defined above. We ask Ninject for an instance of a Samurai. Now, when you ask the Samurai to attack, you will see it has been given an array of all the types bound to IWeapon. class Program { public static void Main() { Ninject.IKernel kernel = new StandardKernel(new TestModule()); var samurai = kernel.Get(); samurai.Attack("your enemy"); } } And you’ll see: Stab your enemy to death Slice your enemy in half The kernel also exposes a GetAll method which lets you generate the same output by doing: class Program { public static void Main() { Ninject.IKernel kernel = new StandardKernel(new TestModule()); IEnumerable weapons = kernel.GetAll(); foreach(var weapon in weapons) Console.WriteLine(weapon.Hit("the evildoers")); } } Continue reading: Object Scopes Enkari Ninject is the illegitimate brainchild of Nate Kohari. Copyright ©2007-2012 Enkari, Ltd and the Ninject project contributors.