Charlotte M-V-VM Code and Decks

Please forgive me for a silent blog and being tardy in posting this code.  I’ve been working late every night.  (In fact I didn’t open Halo Reach until late tonight.)

I have not completed a video series for this yet, but attendees were writing me asking for the code and decks, so I’m posting a advance copy for them now.

Information

If you did not attend the classes, the I’m working on a series of videos on M-V-VM that will help with understanding M-V-VM and the code here.  This series will also cover advanced M-V-VM scenarios.

These applications were used to demonstrate different coding techniques and to show the value of adopting M-V-VM.  The last application takes the M-V-VM application to another level by breaking it apart into granular pieces and composing those pieces at run-time.  Additionally, the Unity IOC Container is used for dependency resolution.

Included in the download are the three applications and associated common libraries I used for the class:

  • Basic WPF Application
  • Basic M-V-VM Application
  • Prism M-V-VM Application

Basic WPF Application

This is a drag, drop; double click, write code in the code-behind.  The application is fully functional with code wrapped properly in try/catch blocks.  For a simple one screen application, this coding style can get the job done, however, when the application starts growing in size, complexity and features, this coding style can quickly run out of steam and get messy.  This application is not easily tested without UI Automation.

Basic M-V-VM Application

In this version of the application the UI has been broken into shell and three UserControls.  A single ViewModel sits at the top of the application tree and is the shell’s DataContext.  The three UserControls bind to this ViewModel.  This application is testable.

The MainWindowViewModel is testable and tests are included.  The ViewModel has two constructors, an empty and overloaded one that is used for testing. 

I’ve used the below pattern before.  An empty constructor enables creating the ViewModel in XAML and the second constructor enables testing ViewModel in isolation.

public MainWindowViewModel() :
    this(new EventRepository(), new ModalDialogService()) {
}

public MainWindowViewModel(IEventRepository repository, IDialogService dialog) {
    this._repository = repository;
    this._dialog = dialog;
    LoadSearchResults();
}

Again for a small application you could get away with this.  However, there are a good number potential problems and some anti-patterns at work here.

ViewModels really should not know how to create repositories or dialog services.  This is where an IOC Container or MEF comes in handy.

Prism M-V-VM Application

In this version of the application the application has been broken down into small pieces using the Prism Modules and has its dependencies resolved using the Unity IOC Container.

I have broken the application into small composable pieces for demonstration purposes.  A simple application like this does not need to be broken down into small granular pieces, but doing so in a small application makes understanding what is going on much easier.  For example, in a real application I would not have three regions and three ViewModels and would not be using EventAggregator in this small tight scenario.  Again, this was done for purposes of instruction.

I have included a subset of the Prism library from Prism v4 drop 7 in the code download. 

You can get the next and future drops on the Prism downloads page:

http://compositewpf.codeplex.com/releases/view/46046

Look at the “Other Downloads” box on the top/right of the page.  We are releasing a new drop (Drop 8) on Monday 20 Oct 2010.  This drop has documentation, quick starts and reference implementations.

Tool-ability

In this application you notice I’ve used the d:DesignInstance markup extension to provide shape to my various DataContexts.  If you are not familiar with d:DesignInstance please see the two follow blog posts:

How to Enable Using the Binding Builder in WPF and Silverlight Applications

Sample Data in the WPF and Silverlight Designer

Download

Charlotte M-V-VM Code (2.9 MB)

Close

I’m going to add one more project to this when I make the videos.  I’ve been spiking on using Decorators to clean up the repeated code in the ViewModel methods.  I’m liking the results so far, I think you will too.

Have a great day,

Just a grain of sand on the worlds beaches.

5 Responses to Charlotte M-V-VM Code and Decks

  1. regquick says:

    Hi Karl!

    Would you be so kind to post MVVM video series? I was very happy when you announce it but I’m still waiting for it…

  2. jimmygee says:

    Hi Karl. Sorry to bug. I see from your roadmap you are going to be pretty busy. (But – theres always a but). Are the Charlotte videos going topart of the Prism 4 posts?
    I never downloaded the Charlotte decks and code as I was waiting for the full shebang.

    Cheers

    • Jimmy,

      In the Box and the MVVM Training will be posted next week. This includes MVVM video and an interactive MVVM learning tool that runs inside VS.

      Best,

      Karl

Follow

Get every new post delivered to your Inbox.

Join 241 other followers