Tuesday, March 23, 2010

Upgrading Suteki Shop to ASP.NET MVC 2

I spent the last two mornings upgrading my little open source eCommerce application, Suteki Shop, to ASP.NET MVC 2. On the whole it was pretty straightforward with only a few code changes required, which I’ll talk about below. So how do you go about doing the upgrade? Here are the steps I took:

1. I downloaded and installed ASP.NET MVC 2, you can get the installer from here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=c9ba1fe1-3ba8-439a-9e21-def90a8615a9&displaylang=en

2. I read the upgrade notes:
http://www.asp.net/learn/whitepapers/aspnet-mvc2-upgrade-notes/

3. …Which suggested I use Eilon Lipton’s upgrade wizard. Eilon is one of the core developers on the MVC team at Microsoft, so I guessed it would be easiest just to let him do it :) You get his wizard here:
http://weblogs.asp.net/leftslipper/archive/2010/03/10/migrating-asp-net-mvc-1-0-applications-to-asp-net-mvc-2-rtm.aspx

MVC2UpgradeWizard

The wizard ran without any errors. Checking the changes with tortoise svn looked like this:

MVC2UpgradeWizard_TortoiseView

The changes were:

- Suteki.Shop.csproj
      - Changed the project type guid
      - Changed the System.Web.Mvc version to 2.0.0.0 (I needed to copy the new Assembly to my dependencies folder and updated the reference path)
      - Added a reference to System.ComponentModel.DataAnnotations
- Every other csproj file
      - Changed the System.Web.Mvc version to 2.0.0.0 (I needed to copy the new Assembly to my dependencies folder and updated the reference path)
      - Added a reference to System.ComponentModel.DataAnnotations
- Added a load of new jQuery and MicrosoftAjax javascript files to ~/Content/Scripts
- Changed all the MVC references in my Web.config files to point to the new assembly, adds a bindingRedirect from v1 to v2

4. I built the solution and got 6 errors initially

MVC2Upgrade_Initial_build_errors

5. Starting to fix the compile errors, I had to do the following:

-  Had to change some of my HtmlHelper extension methods to use MvcHtmlString instead of string.
-  ModelBindingContext.ValueProvider doesn't have an indexer any more, had to change some code to use the GetValue method.
-  ModelBindingContext.ModelType is now provided by ModelMetadata, so unit tests for custom binders had to change from:

context = new ModelBindingContext()
{
  ModelName = "foo", 
  ModelType = typeof(TestEntity),
  ModelState = new ModelStateDictionary()
};

to

context = new ModelBindingContext()
{
    ModelName = "foo", 
    ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TestEntity)),
    ModelState = new ModelStateDictionary()
};

- Mocking HtmlHelper: the ViewContext constructor now requires a TextWriter argument.

6. Added the new MVC Futures assembly from
http://aspnet.codeplex.com/releases/view/41742

7. Added the latest version of MvcContrib which I downloaded from
http://teamcity.codebetter.com/

I also upgraded all my Castle project references with the ones from MvcContrib.

This was the minimum possible upgrade effort. The next task will be to look at all the nice new features in both the core MVC Framework and the latest version of MvcContrib and see what bits I can use in Suteki Shop.

3 comments:

Keith Bloom said...

That is a useful looking tool. Did you upgrade from MVC 1 or one of the pre-leases?

Mike Hadlow said...

Hi Keith, It was upgraded from MVC 1 RC. I don't know if the tool would work for any other scenarios.

Anonymous said...

Thanks for your continued development on Suteki Shop.