Wednesday, April 07, 2010

ASP.NET MVC

I've been working with ASP.NET MVC for a while now. I really enjoy working with the framework and had a very successful project delivered using it. Some of my highlights are listed below.

Separation of concern
The framework is a wonderfully elegant implementation of the MVC pattern. Once you get your head around the responsibilities of the controller, model and view you can very rapidly produce robust, testable code.

Productivity
There are huge productivity gains available when using ASP.NET MVC. It takes so much complexity away from ASP.NET devlopment, without reducing flexibility and power. When doing ASP.NET development, you need to be aware of the page lifecycle at all times and trying to work out which actions should be performed at which step of the page lifecycle can be extremely complex.

Testability
The framework really lends itself to testability. You have to be a little careful to keep it clean, but if you do, the whole system is very testable. The key point to ensure testability is to only pass ViewModels into your controller actions. I cannot stress how important this is. As soon as you try to access the Request.Form in your controller method it becomes incredibly hard to test. You have to mock out the HttpContext and it just gets messy. Even with some very complex forms, I always found a way to represent it all using ViewModels. If you get stuck, use a custom model binder.

Reliability
This is the area that surprised me the most. We had a very low rate of regression errors. I attribute this to two factors:
1. Separation of responsibility - Each controller method operates quite independently, so changing code should not affect other functionality
2. Testability - Having a good suite of unit tests helps to catch any errors that might creep into the code.

I put together a presentation explaining some of these points in more detail. It's available here.

No comments: