Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a controller factory to ASP MVC

Tags:

asp.net-mvc

I have a design idea for a large project at work and I think I have it figured out but would really love to get some feedback on a) the idea in general, and b) my proposed implementation.

The basic idea is simple: I want to create an ASP MVC application that can be extended in the future with additional controllers and views without having to recompile the code. The idea is to have one MVC application with a very basic set of features and then extend the functionality by adding another 'Application.dll" that contains controllers, data, and business logic that are specific to that application. The views will simply be copied into the same directory as the main MVC application during install.

The problem is that MVC does its routing on types within the same assembly so even if I move the routing definitions to the database, the MvcHttpHandler would not be able to route anything to the new Dll since it doesn't "know" the controller types in it. Looking at the MVC code, I found that to load the controllers they are simply calling Activator.CreateInstance which looks only in the current assembly.

My solution is simple but maybe I'm missing something: I will override the MvcHttpHandler by either replacing the ControllerFactory directly (not sure how to do that) or by duplicating that functionality in a derived class. The new code will read the request and try to load the controller first from the current assembly and then from the extended ones. Once the proper assembly is found, I will use CreateInstance and pass that assembly to it to get the controller I want.

like image 413
Gil Avatar asked Feb 20 '09 03:02

Gil


2 Answers

The end of this article shows how to implement your own ControllerFactory. Basically, you derive from DefaultControllerFactory and then wire it up in Application_Start() in your global.asax.

like image 149
Ben Robbins Avatar answered Nov 08 '22 22:11

Ben Robbins


There is nothing wrong with the idea of a controller factory, as long as it is simply for flexibility later. the problem si when you load up the controllers with code that belongs in other layers, which is one reason you might "need" the flexibility of other controllers. As long as you are not mashing layers togehter, I see no real issue with the factory idea.

like image 1
Gregory A Beamer Avatar answered Nov 08 '22 20:11

Gregory A Beamer