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