Modularity and composition of Asp.Net MVC web site
programming, software, spark, tech January 16th, 2009I added a sample of a Asp.Net MVC based framework a web site composed of individually deployable packages.
As you may know the Spark view engine site is a Drupal CMS. I’ve been amazed how well Drupal and WordPress can compose a site from extensions and plugins. There was an earlier question about how you would do that with Spark, so I recently posted a more complete sample following up on that. The code for the sample is in the .zip download under Samples/Modularity/Modularity.sln.
I don’t know why but this was something I really enjoyed doing and the parts really clicked as it came together. The idea is this - there are a number of things in an MVC application that can be loosely associated with each other because they’re powering an area of a site. Let’s say there’s a route to some controllers, and their views, and resources that are only used on those views. Let’s also say there are some services the controllers use.

Loose Objects
There are a few real-world situations you might want to independently version and deploy this set. You could be making an open source platform designed to be plug-in extensible. Or you could be working on a very large production web site where it’s impractical (even dangerous) to atomically version and deploy the entire width and depth of a multi-team / multi-departmental site.
With a creative use of embedded resources you can compile all of this into an assembly. This example adds the idea of a class that implements IWebPackage which is called by the host for the package to register is component parts. Include a nice Inversion of Control container and packages can also expose and consume services.

Packaged objects in assembly
The SendMailService would probably not be in Store.dll - rather those controllers could expect an ISendMail service to be magically dropped in it’s hands and that service could be implemented in any other package which registers an appropriate component.
The ultimate goal is to have a thin shell of a web application project that has little or no capabilities on it’s own, but which will load all of the packages it can discover. Those packages each contribute routes, controllers, services, views, and other reusable partial views.

Packages and assembly references
It sounds impractical and looks like white-board software - but with a combination of Asp.Net MVC, Spark, and the Windsor container it really works well and strikes me as a simple and practical basis for a site architecture.
Call me crazy - but I really have the urge to write a CMS now. Like a dotnet Drupal clone. Too many things are going on to start that soon but unless I come to my senses I might start a CMS project on Codeplex eventually.
January 17th, 2009 at 1:44 pm
Modularity including the “View” layer… simply awesome!
I have always thought of modularity as something that fits well only in the “back end” code. Considering how plugin architectures are implemented in some of the examples you mentioned (Drupal, Wordpress) this is a HUGE step.
Also as an IoC fanboy I can totally relate how this can come together. Here is an example scenario that I thought about, lets say your site needs comments capability for stories or blog posts. You build a comment module and package it as Comments.dll. Page specific customization can happen at initialization through the IoC container when your webhost request individual packages to assemble the complete page.
My initial thoughts… this is great! It would be fun to implement something in this new “Site Architecture”…
January 17th, 2009 at 6:20 pm
Nice! I have been testing some different approaches to this as well but the way you solved is better :)
January 20th, 2009 at 10:11 am
Thanks! If you have any suggestions or requirements I’d love to hear them.
January 26th, 2009 at 10:07 am
Wow, really great stuff here. Nice work!!
Is there something I can do to get Code highlighting, Intellisense to work with Spark files in the WebPackage? For me, VS is opening them as text files as best I can tell.
January 26th, 2009 at 6:33 pm
Yeah, that’s inconvenient isn’t it?
The Spark VS language service is fitting into the same place the aspx language service would occupy… That’s good because the Intellisense engine is very full-featured but expects the containing project to be a web site or web application project.
But there is good news! A web application project is virtually identical to a class library in almost every way. You could use that type of project for your web packages and delete the web.config, default.aspx, etc.
The .spark file editor should work at that point, and the use of embedded content and the other tricks are the same after that.
January 26th, 2009 at 11:50 pm
Nice post and good food for thought as I’m working through some of this now with BlogSvc http://blogsvc.net.
February 27th, 2009 at 8:09 am
You might be interested to know that Prism2 (modularity bits and Unity Extensions) and ASP.NET MVC work together with just a few changes. ASP.NET MVC apps can then be composed by adding external modules ( from MVC Web Module project template ) to the main MVC web application and adding a module definition to the web.config file.
March 18th, 2009 at 9:14 am
[...] Modularity and composition of Asp.Net MVC web site [...]
March 19th, 2010 at 7:30 am
[...] Louis DeJardin’s post [...]