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.
Showing posts with label ASP.NET MVC. Show all posts
Showing posts with label ASP.NET MVC. Show all posts
Wednesday, April 07, 2010
A strongly typed RadioButtonList for ASP.NET MVC
There are many strongly typed controls in ASP.NET MVC. For example TextBoxFor. I really like using the strongly typed HTML helpers because you get much better information at compile time. Especially if you've turned on compile time view checking.
The one control that was missing, however is RadioButtonList. There is no "RadioButtonListFor" to be found. So I wrote my own.
This is what the code looks like in the aspx
Here's the code in the controller to check what the user has selected
Additionally, at the risk of creating an infinite loop, here's a StackOverflow question that this post is an answer to. It's got some useful comments and more code.
The one control that was missing, however is RadioButtonList. There is no "RadioButtonListFor" to be found. So I wrote my own.
This is what the code looks like in the aspx
<%= Html.RadioButtonListFor(m => m.GenderRadioButtonList)%>
Here's the code in the controller to check what the user has selected
public ActionResult Submit(HomePageViewModel viewModel)
{
if (viewModel.GenderRadioButtonList.SelectedValue == HomePageViewModel.GenderType.Female)
{
// Do something
}
return View("Index", viewModel);
}
The code, plus a sample website using it, is in this zip file.Additionally, at the risk of creating an infinite loop, here's a StackOverflow question that this post is an answer to. It's got some useful comments and more code.
Labels:
ASP.NET MVC,
Development
Subscribe to:
Posts (Atom)