views:

59

answers:

1

I have 2 different concrete objects, lets save ConcreteOne and ConcreteTwo. Each of these implement an interface ILooseyGoosey. I would like ninject to call a different method depending on the attribute on that method.

This is what I have so far:

public class ConcreteOne : ILooseyGoosey
{
  public void SomeMethod() { };
}
public class ConcreteTwo : ILooseyGoosey
{
  public void SomeMethod() { } ;
}
public interface ILooseyGoosey
{
  [CallConcreteTwo()]
  void SomeMethod();
}

This is what I have defined in my Ninject module:

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

I get the error of:

System.NotSupportedException : Error registering service ILooseyGoosey: Multiple default bindings declared for service. Found 2 default bindings:

A: 

The problem is that you are assigning one interface to two implementations without any conditional logic. The logic you are applying is only applied to which methods are injected. Ninject has no idea which binding to use since you are indicating that they are both default.

Ian Davis
Ian - thanks for responding. I know what the issue is but I do not know how to fix it. Do you have any ideas?
Jamie Wright