ASP.NET MVC Unity Controller Factory

原文:http://forums.asp.net/t/1860808.aspx

In case anyone would need an ASP.NET MVC Unity Controller Factory, here is a good working one:

internal class UnityControllerFactory: DefaultControllerFactory 
{
    private readonly IUnityContainer _container;

    public UnityControllerFactory(IUnityContainer container) 
    {
        _container = container;
    }

    public override IController CreateController(RequestContext requestContext, string controllerName) 
    {
        try 
        {
            return (IController) _container.Resolve(GetControllerType(requestContext, controllerName));
        } catch 
        {
            return base.CreateController(requestContext, controllerName);
        }
    }

    public override void ReleaseController(IController controller) 
    {
        _container.Teardown(controller);

        base.ReleaseController(controller);
    }
}


你可能感兴趣的:(IOC,unity,依赖注入)