Whenever the request comes from the users, it is processed in the following sequence in order to render the view to the user.

As we can see from the above diagram whenever a request comes from the user, first it goes to the routing system then it is transferred to the ControllerFactory which internally decides which controller to call and then in turn which action to be invoked is decided by the controller, so that the requested view is returned to the user. In this case every request coming from the user has to go through the ControllerFactory. Now you must be wondering we’d not created any ContollerFactory in our application well that rights ASP.Net MVC provides you with the default ContorllerFactory which does the work of finding the appropriate controller and processing your request. The "DefaultControllerFactory" is the class provided by mvc as a ControllerFactory. It basically implements the interface "IControllerFactory" which has certain methods which does the loading and creation of controller. Following are the methods defined inside "IControllerFactory" interface.
- IController CreateController(RequestContext requestContext, string controllerName)
- SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
- void ReleaseController(IController controller)
Well Microsoft has provided you with the benefit of flexibility so that you can change the default behavior of working with mvc to your own customizable way. Here the example just demonstrate that you can change the default working of ControllerFactory to the way you defined, by just creating a class which implements the IContollerFactory interface. But this is not the ideal way for doing things that because if you try to implement your own ControllerFactory you need to make sure about the various issues that may occur during execution and working of your controller. Issues like
- Properly handling user request and loading the right Controller which is required
- Loading the controller dynamically
- Resolving the ambiguity issues arising because of same class name within different namespaces.
- Handling exception which may occur during runtime
Note
- This is just a demonstration to known/ understand that we can incorporate our own implementation logic in creation of controllers. By creating a class which in turn implements the IControllerFactory interface.
- If you take my view, then the built in "DefaultControllerFactory" is best one to use for some scenarios where the things to be done are not that complex.
- Well some developer makes use of customized controller factory for doing dependency injection via constructors or for writing their own way for processing a request.
Ok now let’s start with the creation of the "CustomizeControllerFactory class". For this I’ve just added a new folder with the name “Infrastructure” in the application.
Here is the code snippet for the same.


Join the conversation! Your thoughts help the community grow.