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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With