Ninject-based binding

Once again, I think that perhaps the obvious is lacking. I am looking for auto-binding by agreements. I watched the Ninject.extension.conventions project and build scanner.

What I have is a lot of lines that look like this: I would like to auto-bind:

Bind<ICommandHandler<MyCommand>>().To<MyCommandHandler>(); Bind<ICommandHandler<MyOtherCommand>>().To<MyOtherCommandHander>(); 

I tried several options:

 Kernal.Scan(x => { x.FromAssemblyContaining<MyCommand>(); x.WhereTypeInheritsFrom(typeof(ICommandHander<>)); x.BindWith(new DefaultBindingGenerator()); }); 

But instances are not returned when:

 kernel.Get<ICommandHandler<T>>(); 
+4
source share
3 answers

try looking at GenericBindingGenerator instead of DefaultBindingGenerator .

+4
source
 // use Ninject.Extensions.Conventions for convention-based binding kernel.Scan(scanner => { // look for types in this assembly scanner.FromCallingAssembly(); // make ISomeType bind to SomeType by default (remove the 'I'!) scanner.BindWith<DefaultBindingGenerator>(); }); 
+1
source

Decision:

 Kernel.Scan(x => { x.FromAssemblyContaining<CoreModule>(); x.BindingGenerators.Add(new GenericBindingGenerator(typeof(IHandleQuery<,>))); x.InSingletonScope(); }); 
0
source

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


All Articles