How to connect the interface in Ninject only if it is not connected yet?

Is it possible to configure Ninject to not bind a dependency if it is already bound.

eg.

If we load a module called Client1, containing:

public class Client1Module:NinjectModule { public override void Load() { Bind<IService>.To<FancyService>() } } 

Then load the module called Base, containing

 public class BaseModule:NinjectModule { public override void Load() { Bind<IService>.To<BasicService>() } } 

We would like to guarantee that BasicService is not connected and the system always uses FancyService. During development, we will not know if a FancyService exists. Client1 module is loaded if it is located.

I really don't want every injection to have a bunch of repeating boiler plate code. Like 50-60 dependencies, everything can be changed in client modules.

Any ideas?

+7
source share
2 answers

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

+4
source

You must make sure that BaseModule is loaded after Client1Module:

  public class BaseModule: NinjectModule { public override void Load() { if (!Kernel.GetBindings(typeof(IService)).Any()) { Bind<IService>().To<BasicService>(); } } } 
+6
source

All Articles