How to Use Ninject Named Bindings with DependencyResolver and PropertyInjection

I understand that constructor injection is preferable, but I'm curious how to use Ninject contextual Named Bindings when using a different form of injection.

In particular, how to do this when using DependencyResolver or nesting properties.

public MyService([Named("Alpha")] IRepository repository) { this.repository = repository; } 
+6
source share
1 answer

You can create named bindings to work on Alpha:

 Bind<IRepository>().To<AlphaRepository>().Named("Alpha"); 

then you can specify others, for example:

 Bind<IRepository>().To<AnotherRepository>().Named("Beta"); 

When your example constructor is used, you will get an AlphaRepository.

To use a name with a property, give the property a name attribute just like you for the parameter:

 [Inject, Named("Alpha")] public IRepository Foo {get; set;} 
+15
source

Source: https://habr.com/ru/post/889072/


All Articles