1
votes

I am using Ninject in my ASP MVC 3 project, I have modified the global.asax file (as normally done) and then created a class NinjectControllerFactory like this:

public class NinjectControllerFactory : DefaultControllerFactory  
    {
        private IKernel ninjectKernel;  
        public NinjectControllerFactory()  
        {
            ninjectKernel = new StandardKernel();  
            AddBindings();  
        }    
        protected override IController GetControllerInstance(RequestContext requestContext,
        Type controllerType)  
        {
            return controllerType == null  
            ? null  
            : (IController)ninjectKernel.Get(controllerType);  
        }  
        private void AddBindings()  
        {
            // put additional bindings here
            ninjectKernel.Bind<IServiceName>().To<ConcreteClass>();
        }  
    }

All this works fine.

Now I want to add my Entity framework context object to the binding, so that I do not have to create a new instance of it for each service.

Can anyone tell how to do it?

Should I create a new interface which just defines a Entity framework context?

Thanks

1

1 Answers

3
votes

You could use self-binding

kernel.Bind<MyDbContext>().ToSelf();

P.S you should also check-out Ninject.MVC3 package on nuget