4
votes

Once again I think I might be missing the obvious here. I'm looking to do auto binding by conventions. I've been looking at the Ninject.extension.conventions project and the assembly scanner.

What I have is a lot lines that look like the following, that I would like to auto bind:

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

I've tried several variations of:

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

But there are no instances returned when:

kernel.Get<ICommandHandler<T>>(); 
3
Have you tried kernel.Load(Assembly.GetExecutingAssembly()); You'd need to put this in CreateKernel overrideWorldIsRound
I'm not sure I understand what you mean here. I have the Kernel.Scan code inside a NinjectModule.Load() method now.Mike

3 Answers

4
votes

try looking at GenericBindingGenerator instead of DefaultBindingGenerator.

1
votes
// 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>();
    });
0
votes

The solution:

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