Home .NET Putting points in the MVC Framework.Controller Factory

Putting points in the MVC Framework.Controller Factory

by admin

This chapter puts a dot in the answer to the question, "How and why does the Controller Factory work?"
Putting points in the MVC Framework.Controller Factory
This is the third article in a series about the inner workings of
MVC Framework. A summary of the previous parts : Should I migrate to the MVC Framework? ; MVC Framework mechanism: Part 1 and part 2 This article will talk about the controller factory mechanism, an important part of the MVC Framework, which is where the entire framework starts.

Why "factory?"

Factory – is the name of a design pattern that defines a mechanism for creating objects (instances of classes)without specifying the classes themselves. For more details, see wiki In the MVC Framework, the controller factory, asthe name implies, is for creating controller objects. Since the information about the required controller comes from the routing mechanism ASP.NET as a stringwith a classname, the controller factory builds controller objects based on this string.

Controller Factory

Normally, a factory creates class objects of a particular interface. For an MVC Framework controller factory, that interfaceis IController. In general, the definition of a controller factory interface looks like this :

public interface IControllerFactory{
IController CreateController(RequestContext requestContext, string controllerName);
void ReleaseController(IController controller);
}
* This source code was highlighted with Source Code Highlighter

Very simple interface :method CreateController should returnobjects that implement an IController, ReleaseController should take care of removing the controller when the controller ceases to exist.
In order for the controller factory to return the required controller instance, it is passed parameters in the form of the name of the required controller
controllerName and the request context requestContext which forms the ASP.NET routing mechanism. The ASP.NET MVC Framework developer does not need to write his own implementation of the controller factory because the framework contains a default factory implementation. This implementation is the class DefaultControllerFactory

DefaultControllerFactory

When the ASP.NET MVC Framework receives a user request, the mapped handler MvcRouteHandler processes it. After finding a matching route, the MvcRouteHandler calls the mapped route handler. The default route handler is MvcHandler which creates and uses the controller factory.
The MvcHandler uses the ControllerBuilder (implemented as singleton ), which always contains a field with a controller factory instance. By default, ControllerBuilder contains and returns an instance of the DefaultControllerFactory class. However, the developer can define their own controller factory by simple code in global.asax :

protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory( new ControllerFactory());
}
* This source code was highlighted with Source Code Highlighter

Where, ControllerFactory – is a custom controller factory class.
So, a controller factory, standard or user defined, is created. After that, it is called with the method CreateController to which the controller class name and the request context generated by the routing mechanism are passed. Based on these parameters, the factory should return an instance of the controller class or raise an exception.

When using the standard controller factory, a set of RequestContext.RouteData.DataTokens parameters can be passed to the request context, among other things, which can contain the namespace names (via the Namespaces parameter value) in which the controller class should be searched. This can be useful and may speed up the application ifyour project has a lot of namespaces and controllers are limited to only one. For details, look for the description of creating routes.

Why and how to define your own controller factory

In fact, redefining a controller factory is a completely optional action for creating MVC Framework projects. However, the developer can benefit from a tool that manages the creation of controller classes.
The most common way to use your controller factory is to implement an IoC/DI container, like Unity , to instantiate controller classes through a container. The need and necessity for an IoC/DI container is a topic for other articles, but ifyou use such containers in your project, overriding the controller factory might be a good way for you to create controllers through a container. You can see a description of such an implementation at this article , here I will only give the class of the custom controller factory created using Unity:

public class UnityControllerFactory : DefaultControllerFactory
{
IUnityContainer _container;
public UnityControllerFactory(IUnityContainer container)
{
_container = container;
}
protected override IController GetControllerInstance(Type controllerType)
{
if (controllerType == null )
throw new ArgumentNullException( "controllerType" );
if (! typeof (IController).IsAssignableFrom(controllerType))
throw new ArgumentException( string Format(
"Type requested is not a controller: {0}" , controllerType.Name),
"controllerType" );
return _container.Resolve(controllerType) as IController;
}
}
* This source code was highlighted with Source Code Highlighter

As you can see the new controller factory inherits from DefaultControllerFactory and overloads the GetControllerInstance which is used in the DefaultControllerFactory to instantiate a controller class by its type.
Another use of the controller factory might be to implement "black" and "white" lists for controllers that can or cannot be invoked through client requests. This might make sense when creating a large project with a mass of controllers that might be out of use for a while. If you implement a controller factory that will check the "black" and "white" lists, you can easily solve the problem of making a controller obsolete at the creation stage. Moreover, you don’t need to edit the code, create new routes, recompile the assemblies or restart the application. It will be enough to edit "black" or "white" list, which can be represented even by xml-file.

Item

We have looked at how the controller factory works. It is important to grasp the following :

  • controller factory is called from the routing mechanism;
  • controller factory creates instances of controller classes;
  • controller factory can be overridden, but it does not have to be;
  • It is better to inherit your instance of the controllers factory from the base class DefaultControllerFactory in order not to break the MVC Framework mechanism;
  • if you use IoC/DI containers, the ControllerFactory will help you a lot.

In the next article we will start to look at the work of the ActionInvoker and related attributes.
Putting points in the MVC Framework.Controller Factory

You may also like