6

I have read online that it is bad practice to use a "kitchen sink" model:

Rule #3 – The View dictates the design of the ViewModel. Only what is required to render a View is passed in with the ViewModel.

If a Customer object has fifty properties, but one component only shows their name, then we create a custom ViewModel type with only those two properties.

Jimmy Bogard's subsequent explanation of how this is good, however, left me a little questioning. It'd be so easy to have my Model just contain a list of Customers, I could even use my POCO's.

So now I get to create custom little view model fragments for every page on the site? Every page that uses a Customer property would get one, but of course could not be shared since some of the information is extraneous, if one page used Age but not Name, for example. Two new mini view model classes right?

This is very time consuming, and seems like it'll lead to a million little custom view models - can someone elaborate as to the utility of this approach and why the easier approach is bad?

Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
  • 1
    I agree with you. View models are great for combining various POCOs. But paring down POCOs that you've already constructed just to achieve a 1-to-1 ViewModel-to-View ratio seems like an unnecessary step to me as well. – Jamey Apr 25 '12 at 14:33

3 Answers3

3

View model class can be used not only to transfer values, but it also defines data types (data annotations), validation rules and relations different then ones used in model. Some advantages that come to my mind right now:

  • There are different validation rules when you change user's password, change his basic data or his subscription setting. It can be complicated to define all these rules in one model class. It looks much better and cleaner when different view models are used.
  • Using view model can also give you performance advantages. If you want to display user list, you can define view model with id and name only and use index to retrieve it from database. If you retrieved whole objects and pass it to view, you transfer more data from database than you need to.
  • You can define display, and editor templates for view models and reuse them on different pages using html helpers. It looks much worse, when you define templates for model POCOs.
LukLed
  • 31,452
  • 17
  • 82
  • 107
  • thanks for the response - but could I add attributes and data annotations to the partial POCO classes as well? – Scott Weaver Apr 25 '12 at 14:37
  • @sweaver2112: You can, but it can result in issues related to class being used in two different views. It is general MVC rule to make separation between model and view layer, but I can say, that using POCOs in simple projects is not a crime. On the other hand, if you jump into habit of creating view models, you will probably find it even easier:) – LukLed Apr 25 '12 at 14:45
2

If you would use your POCO objects as view models, you would essentially be showing your private objects and break the encapsulation. This in turn would make your model hard to change without altering the corresponding views.

Your data objects may contain details that are appropriate only to the data access layer. If you expose those things to the view, someone might alter those values that you did not expect to be altered and cause bugs.

Many of the same reasons as for having private members in OO languages apply to this reasoning. That being said, it's still very often broken because it's a lot of extra work to create all these "throw-away" models that only gets used once. There exists frameworks for creating these sorts of models, though the name eludes me, that can tie objects together and pick out the interesting properties only which takes away some of the drudgery from creating specific view models.

Dervall
  • 5,736
  • 3
  • 25
  • 48
0

Your View Model tells the View how data should be shown. It expresses the model. I don't think its necessary to have two view models unless you have two ways to express your model. Just because you have two pages, doesn't mean you will be showing the data any different way, so I wouldn't waste time making two mini View Models when it can be in one reusable view model, Imagine if later you have a page that needs Name and Age, you would create another view model? It's absolutely silly. However, if you had two pages both showing 'Age' and it needed to be shown in a different way, then I would create another one.

AwDogsGo2Heaven
  • 952
  • 11
  • 26