09
Oct
2007

Observations on Microsoft MVC for ASP.NET

Anyone who's tried to develop large complex web sites with ASP.NET has likely run into many problems covering the page and control life cycle, view state and post backs and subsequent maintainability including the difficulty in creating automated unit tests.

Microsoft, taking a cue from the popularity of Ruby on Rails and subsequent .NET related efforts such as MonoRail, are embracing the model-view-controller (MVC) pattern and developing something more suited to web development than WebForms which aimed to make web development as similar to Windows development as possible (despite the major underlying differences in architecture).

The prototype, currently named System.Web.Mvc or Microsoft.Web.Scalene depending on where you look, is headed by Scott Guthrie with both Phil Haack and Scott Hanselman involved (sounds like a dream project and team to be involved with) and a preview release ("drop") is due within the coming weeks.

Gurthrie and Hanselman presented Microsoft MVC at the Alt.Net conference which revealed some interesting details buried in the video, my rough observations and notes based on the prototype they showed follows:

Philosophy

  • Don't repeat yourself
  • Highly extensible & pluggable
  • Use generics to achieve strong-typing without code generation
  • Good performance, fast enough for large-scale sites
  • Separation of concern for maintainability
  • Clean URLs in and out
  • Clean HTML

Extensible

  • Interfaces used extensively
  • No sealed classes
  • Plug-in points for view engines (e.g. MonoRail's NVelocity, Brail)
  • Support for Inversion of Control (IoC) engines (e.g. Windsor, StructureMap, Spring.NET)

Compatibility

  • Runs on the .NET 2.0 CLR
  • Some helper classes require .NET 3.5 (extension methods)
  • Normal Request, Response objects (via interfaces for mocking)
  • Does not support postback (form runat="server")
  • Supports MasterPages, DataBinding, CodeBehind
  • Existing .aspx's are blocked using web.config

Visual Studio

  • Solution templates for web project and unit testing
  • Full designer & IntelliSense integration

Flow

  • Route -> ControllerFactory -> Controller -> Action -> ViewEngine -> View

Routing

  • Routes can be defined dynamically and support RegEx URL matching
  • IControllerFactory pops out the required IController
  • Routing is case insensitive by default
  • Support REST, blog engine, Jango style mappings etc. default is /controller/action/parameters

Controllers

  • FrontController style
  • IController exposes:
    • Execute(IHttpContext context, RouteData routeData)
    • IViewEngine ViewEngine property
  • Some implementations available, all with virtual methods:
    • ControllerBase (adds dispatching)
    • Controller (Populate parameters into ViewData["key"])
    • Controller<T> (Populates parameters by making ViewData type T)
  • Attributes
    • [ControllerAction] attribute to expose methods as actions (secure default behaviour by not exposing helper methods)
    • Attribute for output caching
    • [ControllerAction(DefaultAction=true)] to override default method of Index

Parameters (to the controller)

  • Automatically parsed where a TypeConverter exists
  • Future versions will support more complex serialization of types
  • Can be nullable - use null coalesce operator for defaults

View Engine

  • IViewEngine
    • IView LoadView(string viewName)
  • Implementations include:
    • WebFormViewEngine

Views

  • IView
    • virtual void RenderView(object data)
  • Implementations include:
    • ViewPage - pick up parameters from ViewData[""] in conjunction with Controller
    • ViewPage<T> pick up parameters from ViewData as type T in conjunction with Controller<T>

HTML

  • Clean HTML generation (have they sorted out the id mangling by MasterPages/INamingContainer?)
  • Static Html class supports Link, PagingLinks and Url methods
  • Map to action names using Lambda expressions to ensure follows refactoring, e.g.
    string url - Html.Link<ProductController>(controller =>controller.Edit(4);
  • Is there a way to follow the default action?

Model/data

  • Pagination extension methods extend IQueryable for getting pages of data (skip, limit)
  • Pattern for view-update-view cycle
  • Object property to form field id mapping available and pluggable, allows
    • product.UpdateFrom(Request.Form)

Testing

  • Easy to write tests by using mock objects (request, response)
  • Unit testing framework project (NUnit, MBUnit, xUnit.NET)

Finally

  • What's with ScottGu's nametag, can the show's organisers not afford anything more than a PostIt note?
  • What cool software is Scott Hanselman using to do the screencast video/zoom/overlay/highlighting?

[)amien


12 responses to “Observations on Microsoft MVC for ASP.NET”


  1. Gravatar 1 steve Oct 9th, 2007 at 17:10

    Talk about being late to the party; everyone else has been using MVC for web applications for about 8 years....

  2. Gravatar 2 Damien Guard Oct 9th, 2007 at 18:10

    Better late than never... and they're serious on soliciting feedback on this.

    [)amien

  3. Gravatar 3 Mark Pollack Oct 9th, 2007 at 20:10

    Hi,

    Great summary. Just a small clarification, Spring.NET's web support is not MVC based. The approach is to still use WebForms, but remove as much of the pain as possible with that approach, i.e. dependency injection for aspx, custom http modules, as well as a better page lifecycle that allows for bi-directional databinding, validation etc. Of course, we will be working to bring added value to the new MVC stack.

    Cheers,
    Mark

  4. Gravatar 4 bonskijr Oct 10th, 2007 at 19:10

    great summarisation,

    if i'm not mistaken ScottHa is using ZoomIt tool from sysinternals suite..

    regards

  5. Gravatar 5 Ben Hall Oct 11th, 2007 at 09:10

    Screencast software I guess is something like Camtasia by TechSmith. I'm sure that records you and the screen at the same time.

    As for the zooming, looks like ZoomIt!

    Can't remember seeing the highlighting.... His theme is available on his blog

  6. Gravatar 6 Ben Scheirman Oct 12th, 2007 at 13:10

    About the clean HTML, ID generation: You get to handle it yourself. I, for one, welcome this change, as the naming containers tend to make th nastiest looking ID elements, just in case they might collide.

    This will allow javascript integration with less... friction.

  7. Gravatar 7 Steve Woods Oct 17th, 2007 at 15:10

    To be honest, the lack of any real MVC for .NET put me off moving solely to it, and I stuck with classic ASP. I built a few projects using Monorail from the Castle Project which brought MVC to .NET but compatibility issues (especially now, with trust problems [in the apps i created] on Win2k3 Server) made me step back as it was a non-standard solution.

    This is a cool step in the right direction, especially for the likes of me who want to use .NET but also like the MVC methodology and do not wish to switch to PHP or other more Linuxy solutions.

    Interesting stuff :D

  8. Gravatar 8 steve Oct 23rd, 2007 at 18:10

    "This is a cool step in the right direction, especially for the likes of me who want to use .NET but also like the MVC methodology and do not wish to switch to PHP or other more Linuxy solutions."

    For what it's worth, Struts (Java) implemented everything in this list in approximately 2000/2001 and real, production systems have been deployed on it, and other systems which have sprung up since as alternative takes on the model. You can basically get MVC frameworks most places except .Net, where once again they're reinventing the wheel.

  9. Gravatar 9 Syed Ali Shah Oct 19th, 2008 at 19:10

    This is very productive step to be taken. Specially the concept of the separation of concerns and eliminating the depedencies should be taken care of. After all the goal should be maximum reusable and extensiblity.

  1. 1 MS MVC - The Empire Strikes Back | travellinghead Pingback on Oct 10th, 2007 at 21:10
  2. 2 ASP.NET MVC Framework announced - Cyberpunk???? Pingback on Oct 15th, 2007 at 09:10
  3. 3 ASP.NET MVC Framework Article Roundup « Jim 2.0’s Blog Pingback on Oct 21st, 2007 at 02:10

Leave a reply




Topics