ASP.NET MVC – How to Make a Modular Site

asp.net-mvcdesign-patternsextensibilitymef

I'm in the planning stage for an employee intranet system to be built with ASP.NET MVC 4. We'd like the site to consist of separate "modules", each of which provides a different feature: messaging, payroll changes, etc. I'd like these modules to be able to be enabled or disabled at compile time. The homepage will display some kind of navigation that will link to each module that is loaded.

That's easy so far, but I don't want the navigation feature to have to know about the modules beforehand. In other words, I want the modules to be dynamically discoverable; I want to be able to write the code for a new module and then have a link added to the navigation bar with no code changes anywhere else in the source. Each module should have some way of registering itself with the navigation bar, and–more importantly–this should be done for each module as it's loaded.

I believe that this precludes using MVC's Areas, since those are designed for the case when the layout of the site is known beforehand. MEF seems like it might be appropriate, although people seem to have had mixed success in combining MEF with MVC. Is MEF actually the way to go here, or is there a better way to accomplish what I need?

Best Answer

I would first have a centralized class that is used for the app to register compiled modules with a static constructor and a static member List of modules that were in the system. The modules would have a static property indicating if its a menu item or not and what order it should appear in the menu.

Each module would have its own static constructor that would note itself in the centralized class that keeps track of modules.

Think of this system more like a time clock system where employees come in and clock in. Then at payroll time, we know to pay all the employees based on who clocked in, etc.

Reflection could also be used if you have an interface contract on the modules they must inherit from that ties meta property info to it.

I worked for Warner Brothers Music and did an internal music processing system for different encoding formats. I made a generic plugin model for encoding with reflection that used inheritance so it could be typecast with reflection to get the basic meta properties of the class. I have not tried using a static centralized class though. Just kind of thought of that randomly as another way to try for fun.

I would also add that I've used MVC serving multiple clients with a baseline of requirement but with also enhanced features similar to what you are trying to do. Instead I converted the MVC into using App_Code rather than requiring a compile. Its easier to push files out this way without needing a centralized compilation.

You can leverage JIT with simple FTP or GIT push rather than needing to compile locally and push DLLs around.

Here is a link to that article on stack overflow

Related Topic