Posts Tagged ‘MVC’

.NET and the elusive MVC pattern

Tuesday, November 18th, 2008

A couple weeks ago, I went to a Microsoft “Heroes Happen {Here}” event.  (Good process should mean that there is no need for heroes, but that is a topic for another blog!).  Anyway, part of the development track was an introduction to MVC for .Net.  MVC has been around the J2EE world for so long, its passe.  However, there has never really been a good implementation of it in the .NET world.

MVC or Model-View-Controller is a design pattern that is used by architects to enforce an enterprise capable design.  The advantage is that the GUI is very light weight – since it is only concerned with displaying data passed to it (the VIew in MVC) and the Model handles all the underlying business/persistance with the all powerful controller being a lightweight bridge between the two.  It scales well, is easily extensible and extremely consistant from one section of code to another – making easy to maintain.

When I started my last project, I looked for a .NET MVC example.  I found a couple articles on the web and 2 books that had dedicated space to it, but none of these examples really seemed to capture the power of MVC.  In fact a couple completely missed the point altogether.

I decided that many people treat the code behind page in .NET as a mini-controller, so maybe it really didn’t make sense to try to apply the MVC pattern in .NET.  There were other ideas floating around.  Martin Fowler’s MVP pattern was a good, but somewhat complex alternative. In the end, I spun my own architecture.

When I heard that Microsoft was coming out with an example of the MVC pattern, I was skeptical.  I, personally, had decided that the MVC pattern didn’t fit within the .NET framework well.  The example they showed at the Heroes event was well thought out.  It’s not 100% ready for prime time, but its close.

What do I mean?  Well, setting up a project for MVC is simple. You choose the MVC project type and Visual Studio works its magic.  As long as you keep your naming conventions and directories straight and don’t put anything into the code behind, it all works great.  However, there is nothing to enforce any of this.  You are relying on developers behaving well.  They will, in the beginning of a project.  By the end of a project, they are more worried about meeting deadlines than following the architecture.  Microsoft said that there next version of MVC may not have any code behind pages.  That will feel a little stange to the hard core .NET developers, but it will make life much easier for the Architect and Team Lead.