Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-wiring for Ninject

Some of the IOC containers have what's called auto-wiring based on conventions, for e.g., IProductRepository maps to ProductRepository without any manual wiring on your part.

Is there such a thing with Ninject?

like image 634
Tib Avatar asked Jan 26 '12 13:01

Tib


2 Answers

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

copied from @Pete Montgomery comment

like image 186
Fernando Mondo Avatar answered Oct 21 '22 05:10

Fernando Mondo


Ninject comes with an extension for convention based configuration. But you still need to configure your convenions. See https://github.com/ninject/ninject.extensions.conventions The syntax has changed for 3.0.0 but has become much more powerful. The following would add bindings for all classes in your system. But normally you want several of these conventions for different kind of classes (e.g. services are singletons, ....)

kernel.Bind(
    x => x.FromThisAssembly()
          .SelectAllClasses()
          .BindAllInterfaces());
like image 45
Remo Gloor Avatar answered Oct 21 '22 05:10

Remo Gloor