Does Ninject automatically introduce unrelated classes?

public class MyController : Controller { private MyClass _class; public MyController(MyClass class) { this._class = class; } } public class MyClass { // stuff } 

My Ninject is connected to input classes that implement IController ( Controller class). But I did not bind MyClass to anything, but Ninject still injects MyClass into MyController .

I think my question is why is he introducing something that I did not associate with anything? Ninject launches class search with MyClass signature? I assume this behavior will be different if MyBaseClass is required for my constructor, and I have two classes in my assembly that inherit from MyBaseClass ?

+4
source share
1 answer

In Ninject V1, ImplicitSelfBinding was a top-level configuration setting (which was true IIRC by default).

In V2, the implicit binding behavior you observe is more deeply connected (although there are ways to disable it - like most Ninject bits, it is very granular and minimal). In V2, the default behavior is that self-binding for specific types is always generated if there is no other binding. The only time you usually do Bind<Concrete>().ToSelf() is to set the binding, for example, to do .InSingletonScope() .

Take a look at this @Remo Gloor answer to disable it in V2 +.

Go do a grep in the source code right now for ImplicitSelfBinding at this point - it's a lot easier to read than rabbit people though!

Also be sure to take a look at Ninject.Extensions.Conventions and tests on ninject.org to host the implicit Bind() ing I*X* to *X*

(As Stephen said, Ninject will not snap to itself if you change your MyClass to abstract .)

+7
source

All Articles