tags:

views:

189

answers:

1

I have an application that is currently divided into Service and Data Access Layers (with an MVC Layer in the works). I'm trying to reduce the amount of boilerplate Ninject code required in my classes.

My first idea was a base class for each Business Object type (i.e. UserBase) that would be extended by UserService and UserDAO. Unfortunately, that would prevent me from extending any other classes (i.e. a AbstractService class extended by all Service implementations, an AbstractDAO class, etc).

My second idea was a simple factory to return the IKernel for various IModule implementations passed to it. Unfortunately, each class would need an IKernel member variable, but it solved the problem and allowed AbstractService and AbstractDAO to be created.

    class NinjectKernelFactory
    {
        private NinjectKernelFactory()
        {
        }

        public static IKernel getKernel(params IModule[] modules)
        {
            IKernel kernel = new StandardKernel(modules);
            return kernel;
        }
    }

Are there any other ideas for optimizing my use of Ninject?

+1  A: 

Yes, using property injection is a good way to go here. And if you want to get an even nicer design, you can even use ninject itself to create the controllers by creating a custom ControllerFactory and using the kernel to get the instance controller. That way, the controller will have been initialized with the IKernel property already:

http://nayyeri.net/custom-controller-factory-in-asp-net-mvc

Joel Martinez