You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the Kernel option InjectParentPrivateProperties is set to true, properties of parent parent classes are still not injected. This leads to very unexpected results when api users want to use a subclass of the default implementation an API provides.
I think, when the previously mentioned option is set, all parent class private properties should be injected, or there should be an option to allow for higher level injection.
A simple reproduction scenario might be as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Security.Cryptography.X509Certificates;usingSystem.Text;usingSystem.Threading.Tasks;usingNinject;usingNinject.Modules;usingNUnit.Framework;namespaceSelfInjectTest{interfaceIA{}publicclassA:IA{[Inject]privatestringName{get;set;}publicstringNameOperation(){returnName;}}classB:A{}classC:B{}publicclassProgram{publicstaticvoidMain(string[]args){IKernelkernel=newStandardKernel(newNinjectSettings(){InjectParentPrivateProperties=true,InjectNonPublic=true});kernel.Bind<string>().ToConstant("Foo");vara=kernel.Get<A>();varb=kernel.Get<B>();varc=kernel.Get<C>();Assert.AreEqual("Foo",a.NameOperation());// PassesAssert.AreEqual("Foo",b.NameOperation());// PassesAssert.AreEqual("Foo",c.NameOperation());// Fails, parent of parent property not injected}}}
The text was updated successfully, but these errors were encountered:
I agree, however i'd vote to remove this feature from Ninject entirely (in a Ninject V4). Private-property injection is basically only limited to cases where bad code is written. I think in 99% of the cases there's a better ctor-injection, or entirely different, design possible.
I can imagine that, but are there any plans for such a new version? If that will still take some time, I think it is still worth the effort to fix this. If you are willing to accept pull requests, I can do it myself as well.
When the Kernel option
InjectParentPrivateProperties
is set totrue
, properties of parent parent classes are still not injected. This leads to very unexpected results when api users want to use a subclass of the default implementation an API provides.I think, when the previously mentioned option is set, all parent class private properties should be injected, or there should be an option to allow for higher level injection.
A simple reproduction scenario might be as follows:
The text was updated successfully, but these errors were encountered: