views:

81

answers:

3

OK, I have been hearing discussion about "ViewModels" in regards to MS's ASP.NET MVC.

Now, that is intended to be a specific kind of Model, correct? Not a specific kind of View.

To my understanding, it's a kind of Model that has a specific purpose of interacting with the View? Or something like that?

Some clarification would be appreciated.

+3  A: 

Essentially Model and View Model are both simple classes with attributes.

The main objective of these classes are to describe (to "Model") an object for their respective audiences that are respectively the controller and the view.

So you are completely right when you say

To my understanding, it's a kind of Model that has a specific purpose of interacting with the View

So, while Model classes are effectively Domain Entities that your application interact with, View Models are simple classes that your views interact with.

Hope it helps :)

Update:

Microsoft has developed a specialized version of the Presentation Pattern by Martin fowler largely based on the Model-View-Controller and called it Model-View-ViewModel (MVVM) for PF application. This pattern is targeted at modern UI development platforms where UI developers have different requirements based more on business logic than traditional developers. Have a look here for a bit of theory

Lorenzo
OK, thanks, and also thanks for the update, that's quite helpful! So, without taking into account MS's special version, with stock MVC 2, do you place ViewModels in a special, designated folder? Or are they essentially just plopped right into the Models folder like any other ones. Or, can you do either?
BOSS
You are welcome. Usually I place models and view models in the same folder because I want to group them together respect to domain to which they refer but that's just my choice and I am sure there are better
Lorenzo
+1  A: 

WikiPedia has a more complete description of Model vs. ModelView than you'll get in an SO answer: http://en.wikipedia.org/wiki/Model_View_ViewModel

I quote ...

Model: as in the classic MVC pattern, the model refers to either (a) an object model that represents the real state content (an object-oriented approach), or (b) the data access layer that represents that content (a data-centric approach).

View: as in the classic MVC pattern, the view refers to all elements displayed by the GUI such as buttons, windows, graphics, and other controls.

ViewModel: the ViewModel is a “Model of the View” meaning it is an abstraction of the View that also serves in data binding between the View and the Model. It could be seen as a specialized aspect of what would be a Controller (in the MVC pattern) that acts as a data binder/converter that changes Model information into View information and passes commands from the View into the Model. The ViewModel exposes public properties, commands, and abstractions. The ViewModel has been likened to a conceptual state of the data as opposed to the real state of the data in the Model.[7]

Hightechrider
While there is a description of Model and ViewModel, that link is just describing the MVVM architectural pattern. Not the differences between Model and View Models
Lorenzo
I has a better description of the differences than the accepted answer :)
Hightechrider
+2  A: 

There is a notion of a ViewModel, but it is not generally associated with Asp.net MVC. MVC uses the Model View Controller patter, where the controller handles interactions, builds up data from the Model, and then passes that data to the View for display.

ViewModels (and the Model View ViewModel pattern) is more generally associated with Silverlight and WPF. Xaml is a bit different in that the views can do two-way binding to the ViewModels, so the technology is a little different. For example, if you bind a textbox to a field, as you type into that textbox, the value of the field is updated dynamically. This sort of interaction isn't really possible in web pages since web pages are stateless.

The similarity in the two patterns is that they are both trying to separate the logic from the display. The most common use/reason for this is testing: you want to be able to perform from code (via a testing framework) all the interactions that a user will invoke via the User Interface.

Travis
+1 for describing similarities between the two patterns that I forgot to mention
Lorenzo
In the book I am reading, "Professional ASP MVC 2", ViewModel is introduced in Chapter 1 as a means of keeping presentation / model interactions both strongly typed and DRY. The microsoft authors include Scott Hansleman, Phil Haack, Scott Guthrie.
Berryl