The world's simplest MVVM architecture


MVVM architectural pattern was introduced to provide segregation, event-driven programming approach and flexible testability of View, ViewModel and Model in WPF/Silverlight applications. MVVM is a special and close variant of MVP (Model View Presenter) design pattern since in MVVM, presenter is replaced by viewmodel. Model communicates with viewmodel with notifications and ViewModel communicates with the view with command binding and data binding. Class Responsibilities and Characteristics The View Class The View class’s responsibility is to define the structure and lookup that user views on screen. It is a visual element, such as a page, user control, window, or data template. It uses the dominant data-binding capabilities of Silverlight/WPF to bind the properties defined in ViewModel to user controls that View is composed of The Model Class Model Class is responsible for data representation. It inherits INotifyPropertyChangedinterface to notify ViewModel if properties defined in Model are modified. It is used to expose the data objects in an appropriate way that those can be easily managed and consumed by View and ViewModel. Objects like IDataErrorInfo are inherited in Model class to provide validations for the properties defined. RaisePropertyChanged method is also added in Model for every property onPropertyChangedEventHandler The ViewModel Class ViewModel in the MVVM design pattern encapsulates the presentation logic and data for the view. It simply holds the data retrieved from data layer and View holds the formatted information, and ViewModel acts like a liaison in between the two. It is never tightly coupled with the View and notifies later, if any property changes in ViewModel. It might take inputs from View and place it on Model, or it might interact with a service to retrieve the model, then translate properties and place it on View. It also exposes methods, commands, and helps to maintain the state of the View, manipulates the Model as the result of actions on the View, and triggers events in View itself. ViewModel always updates Model and properties defined from View having UI level events mapped to commands in ViewModel by two-way data binding

Comments