I have a single Ninject 2 Kernel for my application which contains all bindings. One section of the application needs to have different settings on the Kernel than the rest of the application, but needs the same bindings (that portion is for NHibernate and needs InjectNonPublic = true and the InjectAttribute set). How can a make a Kernel that shares bindings with the current kernel but has different settings?
I believe that in other IOC containers this is would be achieved with a “nested container”, however I don’t see any support for nested containers in Ninject?
I eventually solved my problem. As I said, what I was really doing was trying to customize injection for use by NHibernate. In order to solve this I ended up using Ninject’s internal IOC to replace certain strategies for its behavior.
Internally, Ninject uses an instance of ISelector to determine which constructors it should consider invoking. I reimplemented this class by decorating the standard Selector (I couldn’t subclass the standard and override because none of the methods are virtual). I could then conditionally change the
SelectConstructorsForInjection()method’s behavior.The above took care of essentially doing
InjectNonPublic = truefor the hydrated types. But I still needed to do the equivalent of setting theInjectAttributefor those types. This I did by replacing theHydratingConstructorScorerlike so:I then incorporated these new strategies by creating a special Kernel like so: