Monday, October 15, 2007

Microsoft's ASP.NET MVC Framework

I attended the Alt.Net conference in Austin a few weeks ago. One of the highlights of the weekend was seeing Microsoft announce and demo the MVC framework. Scott Hanselman has the full video recording here.

I have to admit that I wasn't that interested at first. I've done MVC in Struts. I've seen MVC in action in Ruby on Rails. There is already MonoRail in .NET that allows me to do MVC. So a MVC framework in .NET didn't really represent anything new to me. I figured Microsoft's version would be so convoluted that it would be more pain than pleasure.

I have to give credit to Scott Guthrie and his team. They definitely did their research into other MVC frameworks both .NET and in other languages.

Some highlights:
  • Unit testing
  • Provides hooks for extensibility
  • Routing
  • Convention over configuration
I was happy to see that I could unit test controllers without the need of a web container. Yes, Microsoft has written a framework with testability in mind. I was so happy to see Microsoft thinking about testability in an application, and I'm not required to use MSTest!

The framework is highly extensible. Everything conforms to an Interface. It comes with base implementations of the interfaces, but this allows teams to use their own implementation. That means teams will be able to use their prefered IoC container to create controllers. Or use monorail as the view engine if you'd like. Scott even showed an implementation of an attribute-based interface that cached responses from a controller. Open, Interface-based frameworks is something I was used to in the Java world, but hasn't always been the norm in the .NET world without putting stuff into the GAC or jumping through hoops.

Routing pretty much looks like you would do it in Ruby on Rails.
/Controller/Action/Id

As a result, building RESTful applications in the .NET MVC framework is pretty simple. I did ask one question about Routing during the presentation. Did I have to go through the pains of configuring the mapping of every single controller? This was something that really annoyed me in Struts - large configuration files with every single Action class mapping. Microsoft definitely went the way of Ruby on Rails's routes.rb.

I was pleased to get the answer I wanted to hear. The framework's base routing mechanism (again, you can plug your own in) takes convention over configuration. If you have the following:

/UserController

The routing service will look for a UserController class, and use the Index method by default. All that is needed is to tell the framework which namespace/dll the controller classes are in. The idea of convention over configuration is a powerful one. One which has lead many to Ruby of Rails, and will definitely be a plus for those adopting the Microsoft MVC framework.

I can't wait to get a CTP or Beta release of the MVC framework. Goodbye code-behind!