views:

326

answers:

5

What kind of naming convention is appropriate for ViewModel classes?

Example: for HomeController, Index view? HomeIndexViewData doesn't seem right.

+1  A: 

EmployeesViewData.

That's what I use and what I've seen in sample applications as well.

About your added example: Imho the name of the class should specify what kind of data it contains. "....IndexViewData" is rather meaningless. What exactly is displayed on that page? Try to summarize it in 1 or 2 word(s) and add 'ViewData' behind it.

Or else just take the name of the controller and drop the "index". HomeViewData sounds fine to me.

Thomas Stock
A: 

I prefer HomeViewModel, or using the previous employee example, CreateEmployeeViewModel, EditEmployeeViewModel, etc. The idea is that the "ViewModel" emphasises the fact that we're dealing with presentation concerns, and disambiguates the ViewModels from any domain model objects that you may have.

James H
+1  A: 

I try to keep my presentation model names agnostic of the kind of presentation they will be presented with. I may use my model object for an ASP.NET view initially, but later on I may also use it in a WCF or WinForms app. I try to name my models such that they logically describe what they contain, without muddying them up with "ViewData", "ViewModel", "Model", etc.

Examples:

ProductsWithPageInfo
ProductWithAttributesAndTags
ClientAndBillingDetail
UserAccountWithAssociatedGroups

Etc.

jrista
Can you provide some good examples?
ScottS
Edited my answer with some examples.
jrista
What are you doing if you have a page that has multiple of those, say a page with ProductsWithPageInfo that also needs ProductWithAttributesAndTags ?
Alex
Well, these types are already aggregations of other types. For example, ProductsWithPageInfo has an IList<Product>, int PageNumber, and int PageCount. Each presentation "model" object contains whatever is needed to render a particular view. Sometimes, if there is a LOT of stuff in a model, I'll add Etc to the end: ProductAttributesPurchaseHistoryEtc, where the Etc means little details (like page number and count) that don't need to be represented in the name of the class. In the case of your example: ProductsWithAttributesAndTagsEtc
jrista
A: 

I've been using the term Envelope as part of my names, which I started using shortly before I read chapter 1 of the Wrox book and discovered the more commonly-accepted term is ViewModel.

However, these ViewEnvelopes that I create are only for very simple encapsulation when I have two or more (typically unrelated) strongly-typed models I'd like to hand to my View. They do not contain any other functionality---the envelope is intended only as delivery mechanism in this sense, whereas the term ViewModel, for me, seems less descriptive for how I'm using it, and also possibly more ambiguous as to what its real purpose is.

I might create, for example, a CustomerUpdateEnvelope class which exists solely to deliver a Customer object and an unrelated NewsTicker object, for example, to my Customer Update view.

Funka
+6  A: 

I use the following pattern because it's clear and unambiguous :

  • Model : Foo
  • View : FooView
  • ViewModel : FooViewModel
Thomas Levesque