software master at the intersection of technology, science and art

home

download

model-view-viewmodel


Model View Model (mvm) and Model View View Model (mvvm) are the design model for wpf. This is a specialization of the Presentation Model. AS per the accronymn, MVVM, this type of application is composed of:


  • Model - entities which represent the business concepts.
  • View - graphic control(s) which "present". i.e. render, the Model on screen.
  • ViewModel - contains the UI logic - the events, commands and a reference to the Model.


The View is an Observer od the ViewModel. So any change in the ViewModel is rendered in the View. This also can be two-way-binding. In WPF, this is handled by the INotifyProertyChanged interface and firing the PropertyChanged event.

An advantage of MVVM is the easy of testing - since the View is an Observer of the ViewModel, testing of the ViewModel covers a large percentage of the UI testing and can be done using unit tests.


Disadvantageously, data-binding in this architecture is declarative and can be harder to debug if there are a large number of events bound around it. Data-binding performance is usually quite good but in large applications the bindings may be larger then the objects to which they are bound. The type of Bindings should be examined to insure that unnecessary Bindings are not defined - i.e. single one way versus two-way.


MMVM helps enforce the Single Responsibilty Principle(SRP) as each viewmodel can be design to reflect the needs of a single actor, producing easily extendable code (if SOLID principles are followed)