WPF, is MVVM worth the pain?

23 May 2012

A friend wrote me an email basically asking if the MVVM-approach to WPF applications made sense, They thought it felt like a lot of duplication of code in the View and View Model. In response, I wrote the following email (with minor edits), maybe someone else will get something out of my 10,000 foot view of MVVM. Note: I always thought it should be 10,000 feet view, but it doesn’t’ sound right, does it?

Kenny

MVVM and WPF/Silverlight

MVVM = Model – View – View Model,  which probably should be
VVMM = View – View Model – Model, but it would be a confusing acronym
Model = Data or the person class in your example.
I think of the View Model as the controller ala MVC from the Smalltalk days, but …  It can do all of the ‘glue’ of Model to View. MVVM is a nice separation of concerns and when you get the hang of the separation, it will feel better…..I think.
The ‘goal’ of MVVM is that the View is “pure XAML”, you should break that rule seldom or not at all, but when it it makes more sense to you to put code behind the View break the rule…ie don’t go nuts. I find that the WPF Commanding architecture is best hosted in the View Model and then tying the View and View Model together is pure Bindings in the View.
One design that I’ve seen work for simple apps. Is a shell of an App that you dynamically load Views into the main container and tie up the VMs. BUT until you get the hang of that, it will feel weird and disconnected. XAML binding problems can be tough, but VS is getting better at it 2010 was a *big* improvement.
If you want, one of the properties of the VM can be the Model or more than one Model say Person.
public class MyViewModel
{
...
    public Person Person { get; private set; }
...
    void SomePrivateCodeProbablyInResponseToACommand( object sender, EventBlah e )
    {
        this.Person = Person.Get( dbcontext );
    }
...
    public ICommand SavePerson { get; private set }
...
    void Init()
    {   // tie up Commands to events/lambdas</div>
    }
}
You don’t need a framework, but when/if you do, MVVM Light, by Laurant, is very nice and offers a bunch of utilities that will be useful when you hit the hard problems (threading, etc..). It includes some nice VS add-ins if you like Wizards and templates.
Kenny
Advertisements

2 Responses to “WPF, is MVVM worth the pain?”

  1. sourcetonuts Says:

    I read this article this morning on the alphabet soup of MVC, MVVM, MVP, MIDK, MWHATEVER, … I’m not religious about these MTF things and this article explains the different architectural approaches without any attitude.

  2. Jeff Circeo Says:

    I’m glad to see your tech blogging, I’m going to send you an email now.

    While I do like MVVM Light, I’m a bigger fan view model first MVVM and the best framework I’ve found for doing this is caliburn micro.

    http://caliburnmicro.codeplex.com/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: