5 best practices for building ASP.NET MVC web sites
Since ASP.NET MVC 2.0 is included with Visual Studio 2010, here is a list of "lessons learned" from building three systems from scratch using ASP.NET MVC.
Since developing with ASP.NET MVC pre-1.0, I've been building a hefty library of ASP.NET MVC 1 & 2 routines that includes everything from blogging to forum modules to CRM libraries.
Visual Studio 2010 shipped with ASP.NET MVC 2.0. If you are a new developer to ASP.NET MVC, it may be daunting at first, but definitely dig in and give it a try. I would recommend it over WebForms development any day (ViewState? Buh-Bye).
When starting out with ASP.NET MVC, some new developers may be looking for some tips and tricks on how to write proper ASP.NET MVC web apps.
Here are some lessons that I've learned the hard way.
- Create really thin controllers and fat models
One thing I'm extremely impressed with is the lack of code in controllers and the large amount of code in the models. If you code was written properly, then your code is definitely "mobile," or reusable, in other systems.
- Build your views with no logic in them
This includes "if" statements and the Law of Demeter with lazy-loading. Customer.Orders.Products is a no-no. If you place this piece of code in your View, there will be at least two calls to the database: one for the orders and one or more for the products (depending on the order).
The best thing to do is add a new collection (or list) of items to your ViewModel and let the controller add it to your ViewModel. Your logic should be located in your models, NOT view models.
- Use Application Caching when necessary
One of the things I've quickly learned in my experience is to immediately notice application bottlenecks. A quick way to fix numerous calls is to use a static list when loaded, so an extra trip to the database is not required.
Also, some of the latest and greatest ORMs already have the innate ability of caching automatically. Use a solid ORM like NHibernate or Entity Framework (or LINQ-to-SQL) to access your data efficiently.
- Create aggregate ViewModels for complex objects
Make sure that you aren't using ViewData and use a custom ViewModel instead. It may be more work, but it is definitely worth it.
- Create HtmlHelpers and UrlHelpers
When I was building an HTML Pager for a grid, it took me a while to build the HtmlHelper because of all of the TagBuilder components. It is extremely helpful to make one call to a generic pager routine instead of coding a lot of HTML pages that you need to touch. I would rather change one piece of code as opposed to every page that has the HTML Pager on it.
Also, the UrlHelpers almost serve as a table of contents for your application. If you need an example, check out Mr. Manzur's best practices (Part 1 and Part 2) if you haven't already.
There are other techniques that I'll write about in a future post. If this post is popular enough, I'll start posting more ASP.NET MVC tips.
Does anyone have any other best practices or lessons learned from building their own ASP.NET MVC applications? Post your comments below.