Search code examples
c#architectureninjectninject-2

Post-initialization object creation with ninject


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.


Solution

  • Create a factory interface

    public interface IClientFactory
    {
        IClient CreateClient();
    }
    

    For Ninject 2.3 see https://github.com/ninject/ninject.extensions.factory and let it be implemented by Ninject by adding the following configuration.

    Bind<IClientFactory>().ToFactory();
    

    For 2.2 do the implementation yourself. This implementation is part of the container configuration and not part of your implementations.

    public class ClientFactory: IClientFactory
    {
        private IKernel kernel;
        public ClientFactory(IKernel kernel)
        {
            this.kernel = kernel;
        }
    
        public IClient CreateClient()
        {
            return this.kernel.Get<IClient>();
        }
    }