What is the difference between the ResourceControllerFactory and the DefaultControllerFactory in ASP.NET MVC?

370 Views Asked by At

What is the difference between the ResourceControllerFactory and the DefaultControllerFactory in ASP.NET MVC?

Context: I am looking to hook an IOC container into the controller factory - I have previously overriden the DefaultControllerFactory to do this, but I see the ResourceControllerFactory being used in a project I am currently working with.

Does one provide improved support for REST APIs?

Code for each:

DefaultControllerFactory

public class DefaultControllerFactory : IControllerFactory
{
    // Fields
    private IBuildManager _buildManager;
    private ControllerBuilder _controllerBuilder;
    private ControllerTypeCache _instanceControllerTypeCache;
    private static ControllerTypeCache _staticControllerTypeCache;

    // Methods
    static DefaultControllerFactory();
    public DefaultControllerFactory();
    internal static InvalidOperationException CreateAmbiguousControllerException(RouteBase route, string controllerName, ICollection<Type> matchingTypes);
    public virtual IController CreateController(RequestContext requestContext, string controllerName);
    protected internal virtual IController GetControllerInstance(RequestContext requestContext, Type controllerType);
    protected internal virtual Type GetControllerType(RequestContext requestContext, string controllerName);
    private Type GetControllerTypeWithinNamespaces(RouteBase route, string controllerName, HashSet<string> namespaces);
    public virtual void ReleaseController(IController controller);

    // Properties
    internal IBuildManager BuildManager { get; set; }
    internal ControllerBuilder ControllerBuilder { get; set; }
    internal ControllerTypeCache ControllerTypeCache { get; set; }
}

ResourceControllerFactory

public class ResourceControllerFactory : IControllerFactory
{
    // Fields
    private IControllerFactory inner;
    private const string restActionToken = "$REST$";

    // Methods
    public ResourceControllerFactory();
    public ResourceControllerFactory(IControllerFactory inner);
    public IController CreateController(RequestContext requestContext, string controllerName);
    public void ReleaseController(IController controller);

    // Nested Types
    private class ResourceControllerActionInvoker : ControllerActionInvoker
    {
        // Methods
        public ResourceControllerActionInvoker();
        protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName);

        // Nested Types
        private class ResourceErrorActionDescriptor : ActionDescriptor
        {
            // Fields
            private ControllerDescriptor controllerDescriptor;
            private string message;
            private HttpStatusCode statusCode;

            // Methods
            public ResourceErrorActionDescriptor(ControllerDescriptor controllerDescriptor, HttpStatusCode statusCode, string message);
            public override object Execute(ControllerContext controllerContext, IDictionary<string, object> parameters);
            public override ParameterDescriptor[] GetParameters();

            // Properties
            public override string ActionName { get; }
            public override ControllerDescriptor ControllerDescriptor { get; }
        }
    }
}


Expand Methods
1

There are 1 best solutions below

0
On

The ResourceControllerFactory offers automatic response serialisation according to the accept HTTP header (I think).