Tech Brief


Is the new MVC pattern right for you?

The Model-View-Controller (MVC) design pattern and Test-Driven Development (TDD) drive the majority of today's enterprise-scale Web applications. The MVC pattern enforces separation of concerns by dividing applications into a model to store and manage business data, views to render and enable the user to interact with data, and controllers to translate user actions to an appropriate set of the model's business data and its view.

A primary benefit of the MVC pattern is that it simplifies TDD for Web apps by eliminating the need to interact with complex GUIs. TDD requires developers to create granular unit test cases, verify the test cases fail when not implemented, write code to pass the unit test cases, and then refactor the code to handle required changes.

Until late 2007, the only viable MVC framework for ASP.NET was open source MonoRail. The Apache Struts framework has provided an MVC architecture for Java since 2000. PHP has more than 10 MVC frameworks. Ruby on Rails implements MVC by default and includes the text/unit library for integrated unit tests.

The Visual Studio (VS) team also came late to the TDD table by failing to deliver unit test and refactoring features with VS 2002 or 2003. VS 2005's Team Test Edition finally integrated MSTest into the IDE and VS 2005 Standard and higher editions added refactoring features for C#. VS 2008 Professional Edition and higher now integrate MSTest, but Microsoft released VS 2008 and ASP.NET 3.5 to manufacturing without MVC support.

Postback model
[click image for larger view]
Postback Model

Scott Guthrie, Microsoft's newly anointed corporate vice president in the .NET Developer Division, previewed the first cut of an MVC framework for ASP.NET 3.5 in October 2007. At the time, Guthrie promised to "ship it as a fully supported ASP.NET feature in the first half of next year."

An MVC community technology preview (CTP) followed in December as part of the ASP.NET 3.5 Extensions. The Extensions included ASP.NET Dynamic Data -- a Rails-like scaffolding framework that enables rapid app development of data-driven MVC sites. LINQ to SQL or LINQ to Entities can serve as the data source for the model.

Earlier this month, Microsoft released ASP.NET MVC Preview 2 at MIX08. It provides MVC routing and testing improvements, the first VS 2008 template support for MVC development in the IDE, and Dynamic Data enhancements.

Key Features
The ASP.NET MVC framework features of most importance to Web developers, according to Guthrie, include the following:

  • The .NET MVC framework integrates deeply with the ASP.NET runtime and uses familiar authentication, authorization, security, caching and configuration features.
  • The architecture is modular, so developers can rip-and-replace individual components.
  • .NET MVC uses familiar *.ASPX and *.ASCX files for views to generate the HTML for views and supports VS 2008's nested master pages.
  • Instead of URLs mapping to *.ASPX files, URLs map to controller classes, which are standard classes that don't incorporate UI code.
  • .NET MVC ships with System.Web.IHttpRequest and IHttpResponse interfaces; this simplifies mocking the interfaces to deliver improved unit-test performance.
  • Instead of writing tests to do Web requests, unit tests can run directly against the controller.
  • You can perform unit testing against classes without having ASP.NET running on the test machine.

View Request Routing Conventions
The standard ASP.NET MVC Project template adds a Global.asax.cs file to your project that defines a public Global class, which inherits from System.Web.HttpApplication. The class includes an Application_Start() method to define RouteTable.Routes for Default.aspx and [controller]/[action]/[id] sub-paths. It's a common convention to assign the model's entity collection names as MVC controller name prefixes and view folder names. ASP.NET MVC follows Rails' convention over configuration lead; MVC's default URL routing conventions treat the URL sub-path that follows the controller name as the request's action name.

Using the Microsoft Entity Framework's Entity Data Model for the Northwind database as an example, a /Products/Categories sub-path interprets Categories as the action name and invokes the Categories() method to let the user browse all Product categories. /Products/List/Beverages calls the List() method to display a list of Products in the Beverages category, and /Products/Detail/34 displays a detail view of the Product entity with a key value equal to 34. Alternatively, URL query strings can specify the category or product key. Route validation rules use regular expressions to test incoming URLs for validity.

It took the Visual Studio team more than five years to read the MVC/TDD handwriting on the wall and deliver its first MVC candidate for ASP.NET. Initial developer reaction has been very positive. ASP.NET MVC already has four alternative view engines, one of which is a port of MonoRail's Brail. MVC promises ASP.NET developers freedom from the increasingly onerous constraints of the page lifecycle and ViewState/Postback model.

About the Author

Roger Jennings is an independent XML Web services and database developer and writer. His latest books include "Special Edition Using Microsoft Office Access 2007" (QUE Books, 2007) and "Expert One-on-One Visual Basic 2005 Database Programming" (WROX/Wiley, 2005). He’s also a VSM contributing editor and online columnist and manages the OakLeaf Systems blog. Jennings’ Code of Federal Regulations Web services won Microsoft’s 2002 .NET Best Horizontal Solution Award. Reach him at

comments powered by Disqus
Most   Popular
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.