Is it possible to configure Ninject to not bind a dependency if it is already bound.
E.g.
If we load a module say called Client1 containing:
public class Client1Module:NinjectModule
{
public override void Load()
{
Bind<IService>.To<FancyService>()
}
}
Then we load a module called Base containing
public class BaseModule:NinjectModule
{
public override void Load()
{
Bind<IService>.To<BasicService>()
}
}
We would like to ensure that BasicService isn't bound and the system always uses FancyService. We won't know at design time whether FancyService exists. Client1 module is loaded if it is located.
I don't really want a bunch of repeitive boiler plate code around every injection etc. As there are 50-60 dependencies that all could be changed in Client modules.
Any ideas?
You have to make sure BaseModule is loaded after Client1Module:
public class BaseModule: NinjectModule
{
public override void Load()
{
if (!Kernel.GetBindings(typeof(IService)).Any())
{
Bind<IService>().To<BasicService>();
}
}
}
If I assume I load the base Module first and then load the Client Module after I think I can just use
Rebind<IService>.To<FancyService>()
It seems to work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With