Does Ninject automatically inject non-bound classes?

走远了吗. 提交于 2019-12-01 06:10:46
Ruben Bartelink

In Ninject V1, ImplicitSelfBinding was a top-level config setting (which defaulted to true IIRC).

In V2, the implicit self binding behavior you observe is more deeply wired in (though there are ways of switching it off -- like most bits of Ninject, it's very granular and minimal). In V2, the default behavior is that self-bindings for concrete types are always generated if no other binding is present. The only time you typically do a Bind<Concrete>().ToSelf() is to customise the binding, e.g., to do a .InSingletonScope().

See this answer by @Remo Gloor for a way to turn it off in V2+.

Go do a grep in the source right now for ImplicitSelfBinding this instant - it's far easier to read than people rabbiting on though!

Also dont forget to have a look at Ninject.Extensions.Conventions and tests on ninject.org for arranging implicit Bind()ing of I*X* to *X*

(As Steven alluded to, Ninject would not self bind if you changed your MyClass class to be abstract.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!