I’m new to Ninject (and DI in general).
I understand how the kernel loads modules, and the code I’ve written thus far tends to have a single line:
myKernel.Get<MyApp>()
which constructs everything I need from the bindings in my module. If there’s a requirement for new instances post initialization, these are taken care of by factories that I bind for initialization. Up to now, the factories have been free of any ninject dependencies, simply newing up objects on demand.
Now I have reached a point that I need to think about object creation after initialization and my own factory pattern is not cutting it any more. This would be to support a pub/sub interface for (remote) clients. With every new connection to my server, I would like to create new IClient instances according to a set of bindings defined in a ninject module. Does this mean that the factory I pass in at initialization has to have its own kernel (or a ref to the main kernel)? Where would CommonServiceLocator feature in this. Is CSL necessary?
Before I travel too far down dead-ends, I thought it would be best to ask here about how others might approach this problem.
Create a factory interface
For Ninject 2.3 see https://github.com/ninject/ninject.extensions.factory and let it be implemented by Ninject by adding the following configuration.
For 2.2 do the implementation yourself. This implementation is part of the container configuration and not part of your implementations.