Click here to Skip to main content
15,860,972 members
Articles / Web Development / ASP.NET

Three Reasons Why You Should Use View Models

Rate me:
Please Sign up or sign in to vote.
4.73/5 (11 votes)
10 Jul 2011LGPL32 min read 37.1K   13   2
A summary of all my answers focusing on View Models in ASP.NET MVC

I’ve answered a couple of questions at StackOverflow regarding the benefit of View Models. Here is a summary of all my answers focusing on View Models in ASP.NET MVC.

Reason 1: Remove Logic From Your Views

When you start working with ASP.NET MVC, you’ll most likely ask yourself why you should use a View Model. Using your domain model or entity model works perfectly fine. And it does. For a while. But as you continue to use your Models, you’ll discover that you have to add some adaptations in your Views. Here is a typical usage adaptation (using the Razor View engine):

Hello @model.UserName

Your age: @(model.Age != 0 ? model.Age.ToString() : "n/a")

Not so bad, is it? The problem is that you have introduced logic into your View, which is bad for two reasons:

  1. You cannot unit test that code, and the only way to make sure that it works is user testing.
  2. You need to repeat that code for every View that intends to use your Model (code duplication = code smell).

All those small adaptations will lead to a big mess eventually.

Reason Two: Security

One of the biggest advantages with View Models is removing security risks. Your database objects or domain objects will most likely contain properties that the user should not be able to change. It can be a property called IsAdmin or Reputation.

All those properties will automatically be changed by the model binder if they exist in the model (and are posted in the FORM, which is quite easy to do if you know HTML). Simply remove them from the View Model and they’ll never be changed.

Reason Three: Loose Coupling

By using domain models or entity models, you are adding coupling between your lower layers and the presentation layer, and that is seldom good. Google “loose coupling” to find out more.

Basically, it means that if you change your domain/entity model, you have to change all your Views that use that Model. If you use a ViewModel, you only have to change the code that maps between an entity/domain model and the View Model.

How to Make the Model Mapping Easier

I usually use a framework called AutoMapper to manage the mappings between View Models and other Models. AutoMapper and its alternatives usually let you use a single line of code to do the mapping.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Founder 1TCompany AB
Sweden Sweden

Comments and Discussions

 
GeneralMy vote of 5 Pin
Victor Wheeler7-May-12 2:56
Victor Wheeler7-May-12 2:56 
QuestionComprehension Pin
kaneXtreme12-Jan-12 23:32
kaneXtreme12-Jan-12 23:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.