He will throw this exception because Ninject will have to instantiate an object of type BProvider . To do this, he must fill out the IA dependency. But the moment ... are there two bindings on IA that I have to choose? ....
You can use some conditional binding to decide which implementation should be used. One way: NamedAttribute :
Bind<IA>().To<A1>(); Bind<IA>().To<A2>().Named("SecondImpl"); Bind<B>().ToProvider<BProvider>(); class BProvider : Provider<B> { IA _a; public BProvider([Named("SecondImpl")]IA a) { _a=a; } protected override B CreateInstance(IContext context) { return new B(_a); } }
In this case, A1 will be entered by default if the NamedAttribute not specified.
or as @Remo Gloor explains in this post: Configuring Ninject
Bind<IA>().To<A1>(); Bind<IA>().To<A2>().WhenParentNamed("NeedSecondImpl"); Bind<B>().ToProvider<BProvider>().Named("NeedSecondImpl");
This is a bit cleaner because you donβt need to bind the code to Ninject and just allow it to be configured (in one place).
mipe34
source share