WPF Good Practices

1. Use a MVVM/MVP approach

Some reminders:
  1. The WPF framework was built around the Model-View-ViewModel (MVVM) pattern fn1
  2. The MVVM pattern is a specialized Model-View-Presenter (MVP) pattern fn2
  3. There are many existing MVVM frameworks for WPF fn3

Reason of the remarks:
  1. Using an other approach is possible (like a classic Windows Form approach) but you will miss some useful features of WPF that increase the modularity and simplicity of your application
  2. I prefer to speak about MVP instead of MVVM - essentially for code writing reason - but we speak about the same thing
  3. There are really many existing good MVVM frameworks for WPF ! But you don't need them to create a MVVM application - I remind you the point 1 "The WPF framework was built around the MVVM pattern" !

The simpliest way to implement a MVP/MVVM architecture is:
  1. To implement the INotifyPropertyChanged on all the ViewModel/Presenter fn4 classes - by providing an abstract base class like AbstractPresenter
  2. Use ObservableCollection or CollectionView for all the collections of the ViewModel/Presenter classes
  3. To provide the available commands as components of your ViewModel/Presenter classes
  4. To implement your own ICommand class to link commands to ViewModel/Presenter methods - like the CommandPresenter class

fn1: Here are some really interesting links about the subject:
- The presentation of the MVVM pattern by John Gossmann (2005) - http://blogs.msdn.com/b/johngossman/archive/2005/10/08/478683.aspx
- An article from Josh Smith (2009) - http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

fn2: In fact, the difference between MVP and MVVM is that the ViewModel is unaware of the View (thanks to the binding for that) - in the MVP model, the Presenter should have a link to the View.
But you should consider to read the article of Josh Smith that gives you more clues.

fn3: Oh yes! there is really many many framework! Doing a simple search on Internet to confirm the fact :)
- On codeplex: http://www.codeplex.com/site/search?query=WPF%20MVVM%20Framework

fn4: This is where you should see why I prefer using Presenter instead of ViewModel.
In fact, if you manipulate a concept called SpecialView, with MVVM notation you will create the SpecialViewModel and SpecialViewViewModel classes, quite odd isn't it? With the MVP notation the classes will be SpecialViewModel and SpecialViewPresenter.

Last edited Sep 5, 2010 at 8:11 AM by Faz, version 56


No comments yet.