The representation model in the MVVM pattern encapsulates the logic of the representation and the data to be displayed. It has no direct reference to the view or any knowledge of the view’s implementation or type. The view model implements properties and commands to which the view can bind data and notifies the view of any state changes via notification events. The properties and commands that the view model provides define the functionality relied upon by the UI, but the view defines how that functionality is to be represented.
Typically, the view model defines commands or actions that can be represented in the UI and invoked by the user. A typical example is when the view model provides a Submit command that allows the user to submit data to a web service or data repository. The view can present this command as a button so that the user can click it to submit the data. Typically, when a command becomes unavailable, its associated UI view becomes disabled. Commands provide a way to encapsulate user actions and cleanly separate them from their visual representation in the UI.
In summary, the view model has the following key characteristics:
- The view model is a non-displayable class and does not inherit from any WPF or Silverlight base class. It encapsulates the view logic needed to support user actions in the application. The view model is testable independently of the view and model;
- The view model usually does not directly reference the view. It implements properties and commands to which the view can bind data. It notifies the view of any state changes via notification events through the INotifyPropertyChanged and INotifyCollectionChanged interfaces;
- The view model coordinates the view’s interaction with the model. It can transform or manipulate data so that it can be easily used by the view, and it can implement additional properties that may not be present in the model. It can also implement data validation through the IDataErrorInfo or INotifyDataErrorInfo interfaces;
- The view model can define logical states that the view can visually present to the user.