www.planet-xaml.net
[WPFArchitecture]

WPF - DM-V-VM or M-V-P - WTF?

by Florian Krüsch October 25, 2007

In the early days of WPF (they are over now, today is Silverlight), there was an app called Microsoft MAX that combined photo-gallery with an RSS reader and looked pretty neat.

Dan Crevier, a member of the team at Microsoft, who build MAX, then went and described the underlying design pattern, which he called the DataModel-ViewModel-View pattern.

Microsoft MAX

You can read all about it here.

It took a while until I realized that this is an incarnation of what Martin Fowler describes as PresentationModel.

What's special about DM-VM-M is that View and ViewModel are glued together using DataBinding quite heavily. DataBinding is a very powerful concept in WPF and this is a nice way to do it.

Basically what DM-VM-M gives you is something that is very close to the view, but separated and testable: a model of the view. The VM also contains the logic that talks to the data-model and that's why I would not choose to implement it for more complex types of apps.

Instead, what I favor is a variation of the Supervising Controller Pattern, which Martin Fowler describes here.

A Supervising Controller type Model-View-Presenter architecture fits very well with the powerful databinding machinerie of WPF and allows very good testability of the behavior of an application.

Basically what you do it set the DataContext of the Window or the current view (UserControl) to the object model, so you can bind your data against it. You add Commands in XAML and expose the CanExecute and Executed events in the view interface. It makes sense to add a little bit of translation logic there that removes the Routed- aspect and casts the command parameter to type that's actually data-bound.

e.g.

C#IViewtoggle
interface IView 
{
   event EventHandler<CommandExecutedArgs<Item>> 
                    EditExecuted;
   event EventHandler<CommandCanExecuteArgs<Item>> 
                    CanExecuteEdit;

   void ShowEditDialog();
}

C#Controllertoggle
class SupervisingController
{
   private IView _view;
   private IDataModel _model;

   
// ... handle view events
   
// ... handle data model events

   
// ... tell view what to display
   
// ... tell datamodel what to do
}

DM-M-VM does have its place, but it's not the only way to design WPF apps. In fact I would use DM-M-VM for simple apps where you want to maximize testing on the view. If there's a bit more logic involved, I'd go for a Supervising controller approach, where the view basically manages itself and the presenter encapsulates the logical behavior.

cheers,
Florian

 

Comments:

  • favicon.ico Wenn sich DM-M-VM in der Hauptsache durch Terminologie vom (MVVM) Model-View-ViewModel unterscheidet und du es als Variante des geteilten MVP identifizierst, dann fragt man sich langsam - wo bleibt der Unterschied ;-)
    # Jens Peter on November 05, 2007
  • Hm, da habe ich mich wohl unklar etwas ausgedrückt.

    MVVM verfolgt als Derivat von PresentationModel einen anderen Ansatz als MVP (aka Supervising Controller, PassiveView scheint mir bei WPF keine gute Wahl zu sein).

    Mein Punkt ist eigentlich, dass Supervising Controller eine vernünftige Alternative zu MVVM ist, auch für WPF.

    PresentationModel oder MVVM versucht, die Darstellungslogik weitestgehend in ein testbares Model der View zu verlagern. Ein SupervisingController dagegen setzt auf DataBinding zwischen (Domain-)Model und View und handled Benutzeraktion auf einer logischen Ebene.

    Das ist ein etwas liberalerer Ansatz mit etwas weniger Abstraktion, der dabei aber eine saubere "Separation of Concerns" erlaubt.

    Leider kann ich das in Deinem Blog nicht kommentieren, weil Du keine Kommentare erlaubst ;)
    # Florian Krüsch on November 05, 2007

Language | Sprache

All[e]  |  english  |  deutsch
Tags
 

Profile

Florian Kruesch

I am working as a freelance software architect, developer and consultant in Düsseldorf, Germany.

My focus is on Microsoft technologies and .NET 3.0, especially WPF, ASP.net and SQL Server.

I've been a lead programmer on the WPF development of the OTTO Vista Store at SinnerSchrader Studios.

My client list includes DHL, LG and Ogilvy Interactive.