From ASP.Net Webforms to MVC

This post outlines some of my experiences moving from ASP.Net web forms to the new ASP.Net MVC framework coming from Microsoft.  There is plenty of talk about all the advantages of the new framework but it comes at a price.

Should You Use It?

The Microsofties are being quite clear that the new framework isn't for everyone and that the old one isn't going away.  I still use both and which one is better is way too subjective for anyone but you to answer.  Many of my projects will continue to use the old model, particularly if the project sounds like this:

  • Small project.
  • Few technical challenges.
  • Lots of content, little functionality.
  • Fast delivery is a priority.
  • Low budget is a priority.
  • Existing web forms based CMS.
  • Team not experienced with MVC.

This describes a lot of the quick marketing style sites that I do.  For some of my new projects I'm moving the the new MVC framework.  There is a large gray area in between but I will prefer the new framework for the following sorts of projects:

  • Large project.
  • Quality is a priority.
  • Difficult technical problems to solve.
  • Integration with external systems.
  • The code base is expected to live for several years.
  • The code will be reused or deployed in many places.
  • Experienced team or one willing to invest time learning.

Now before I make anyone angry, yes you can do a great job of large complex projects using the web forms model, I just hate doing it.

Learning Curve

If you're like me and you've never worked with a similar framework before (for example MonoRail or Rails) you're suddenly going to find yourself very unproductive as you relearn how to do all the things you've been doing for years in web forms.  Here are some of the things you will want to spend time on:

  • Learning the key architecture concepts in the framework.  You will need a solid understanding of how a URL makes its way through routing, a controller and finally the view.
  • Form input and validation is very different to what you are used to.  Spend time coming up with some patterns you like.
  • Postbacks and viewstate no longer exist as you knew them.  You can still do similar things but it will be a bit more manual than you are used to.
  • You need to understand more about how HTML and HTTP work.  Details such as the difference between GET and POST become important.
  • Many of the existing controls you use are not going to suit the MVC model.  As well as the usual display controls this is going to affect things like AJAX.

Expect to spend a couple of weeks before you are really comfortable working with the new framework.  The good news is that once you figure these things out you will be back to your old self again.

What Helps?

The new framework isn't really based on new ideas.  In fact it's really built on tried and tested concepts, even if they are new to the average Microsoft developer.  Here are some things from my past experience that help ease my transition:

  • Knowing ASP.Net and IIS very well.  Most of your knowledge about these technologies will still be useful.  Though you wont be using web forms you will still have the other ASP.Net features to work with such as session state and output caching.
  • Experience with unit testing.  One of the key goals on the new framework is testability and if you aren't doing it you're probably missing out on a lot of the advantages.  Unit testing can be tough to pick up in a hurry so any knowledge will help.
  • Familiarity with domain driven design.  I had already been working with patterns for a strong domain model distinctly separate from the user interface.  Most of my existing work in this area carried over very smoothly.
  • jQuery (or your JavaScript library of choice).  It will definitely help if you're used to getting your hands dirty with JavaScript as you will find yourself building many features from scratch, features web forms has out of the box.

Working With Beta Bits

The framework is still in beta so it's not done and there will be changes.  The core feels very solid and I haven't encountered any major bugs that have stopped me.  More importantly realise that there is not yet good support from third party libraries to fill in the holes so things feel very bare bones in a lot of cases.  One of the few resources out there is the MVC Contrib library.

Because the framework has already had 6 releases (5 previews, 1 beta) with breaking changes most of the information in blogs and forums is out of date and wont work exactly as described.  Finding information on how things are now can be tricky and keeping Reflector nearby is recommended.  The best information is still in Scott Gu's blog.

Freedom

I'm starting to settle in with MVC and it feels good.  My XHTML validates as strict and I haven't had to do anything remotely resembling a hack or fudge.  The framework is extensible in so many ways that web forms isn't so the few things I haven't liked I've been able to change.

Yes, it's worth it.

15/01/2009 02:20 AM (UTC -08:00)