0
votes

A while ago, I built a ControllerFactory with the goal of use Generic Controllers in my Asp.Net MVC project. This factory is something like this:

public class MyControllerFactory : DefaultControllerFactory
{
    public override IController CreateController(RequestContext requestContext, string controllerName)
    {
        if (/*controller is generic, I know it by a convention in the controllerName*/)
        {
            string typeName = //resolve Type name from controllerName and requestContext;
            Type controllerType = typeof(MyGenericController<>).MakeGenericType(Type.GetType(typeName));

            var ninjecKernel = //get a ninject kernel;
            return (IController)ninjecKernel.Get(controllerType);
        }
        else 
        {
            //build a normal controller
        }
    }
}

I use this controller factory in the Globar.asax file like this ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

But in the services injected by ninject to the controller are InSingletonScope. Now I decided to inject those services InRequestScope, so, the services can share the same EntityFramework context. To archive this, I'm trying to use this approach, when services are injected in request scope, using the Ninject.Web.Mvc.dll. In this approach ninject is who build the controllers internally by inheriting MvcApplication of Global.asax from NinjectHttpApplication, so I don't know how to keep the Generic Controller code.

How can I archieve both goals?

1

1 Answers

0
votes

Don't inherit your Global.asax from NinjectHttpApplication. You will still be able to use InRequestScope to control lifetimes.