Archive

Archive for June, 2010

WPF MVVM Example App

June 8, 2010 14 comments

I’ve been writing a simple app to try out Windows Presentation Foundation (WPF) using the Model-View-ViewModel (MVVM) pattern, so I thought I’d post it in case anyone found it useful – there don’t seem to be many end-to-end examples using MVVM around.  I’ve tried to stick to good practices, although there’s still a fair bit of discussion in some areas about what the best way to do things is.

Before you read any further, if you’re looking for an example of a decent WPF UI, stop now!  I can assure you, you’ll only be disappointed..  I used a theme so it wasn’t grey, but that’s about it.  It’s all about the code!

The Code

The source code is here: https://github.com/relentless/wpf-diet-recorder

You’ll need Visual Studio 2008 and above to use this (I haven’t tried it with 2010, but it should work Ok).

The App

The app lets you record your weight and some notes on the Measurements screen:

measurements screenshot

This can be done for multiple users, which are set up on the Users screen:

users screenshot

(You might notice the Users screen allows you to define custom measurements for the users – these aren’t used anywhere at the moment, as putting that in was making it too complex for this example).

Features

A couple of interesting things in there are:

  • Automated Unit Testing (of course!)
  • Composite views (the ‘custom measurements’ part of the Users screen is a separate user control with its own ViewModel)
  • No code-behind
  • Commands using DelegateCommand
  • ValueConverters

Unit Testing

Although there aren’t as many unit tests as I would like, there should be at least examples of the main things you’d want to test.  (My only excuse for the lack of tests is this was my first WPF app, and as such basically a prototype, so I was working it out as I went along).

I’ve used Roy Osherove’s naming convention for the unit tests (MethodName_StateUnderTest_ExpectedBehaviour), as I like it.  I’ve also stuck with the AAA pattern for the test layout (Arrange, Act, Assert), and when I’ve used Rhino Mocks for test isolation, that’s been in ‘AAA mode’.

ValueConverters

There is still much discussion in MVVM circles about whether ValueConverters should be used, or if the ViewModel in MVVM should render them unnecessary.  I’ve used ValueConverters as I don’t want to have too View-specific things (such as Visibilities and Colours) in my ViewModel, but some people (including Josh Smith, the father of MVVM) disagree.  To be honest, it doesn’t matter much (especially if you’re just starting out), but it’s good to be aware of your options.

Validation

Always tricky to know where to put the validation – in this case, I’ve gone for validating the data type in the ViewModel, and any more complex validation (string lengths, max and min value, etc) in the model itself.

Libraries / Tools

The things I’ve used to build this (aside from VS2008) are:

Further Development

If I was to continue developing this example, there are a couple of things I’d probably want to do:

  • Make some kind of Application Controller, which would contain the ugly startup code from App.xaml.cs and be in charge of opening forms
  • Use an IoC container such an Ninject to resolve those dependencies
  • Probably take the data loading/saving logic out of the ViewModels and find somewhere more appropriate for it

That’s It!

Hopefully someone might find that useful.  If you have any questions, feel free to get in touch.

Categories: WPF Tags: , ,