Charlotte M-V-VM Code and Decks

September 19, 2010

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.


Charlotte Presentation: Creating M-V-VM Applications that are Tool-able

September 2, 2010

On 11 September I’ll be in Charlotte, NC visiting the Charlotte Enterprise Developers GUILD family.

Come and join us for the ½ day event on Creating M-V-VM Applications that are Tool-able, this link is also the registration link. Event is free; registration gets you a seat and food.

Time

Session

8:30 – 9:00

Registration

9:00 – 9:30

M-V-VM Part 1 – The Pattern

9:30 – 10:30

M-V-VM Part 2 – Structuring Applications

10:30 – 10:45

Break

10:45 – 11:45

M-V-VM Part 3 – Application Services

11:45 – 12:00

Break

12:00 – 12:30

M-V-VM Part 4 – Tool-able Applications

12:30 – 1:00

M-V-VM Part 5 – Testing M-V-VM Applications

1:00

Close

M-V-VM Part 1 – The Pattern

During this 30 minute presentation you will get the answers to the following questions:

  • What is M-V-MV?
  • How does the pattern benefit my team and the applications we deliver?
  • Is this pattern a natural fit for XAML applications?
  • Does this pattern enable building tool-able applications?
  • Why is the XAML data binding stack critical to M-V-VM?
  • What about my designers, will they be able to work with this pattern?
  • What does thinking in M-V-VM mean?

M-V-VM Part 2 – Structuring Applications

The Internet is full of information around M-V-VM.  Navigating the information and putting the pieces together can be a bit overwhelming at first, but it doesn’t have to be.  At the end of this 60 minute presentation you will understand:

  • What is View first?
  • What is ViewModel first?
  • When do I use View or ViewModel first? 
  • Can I have multiple Views for a single ViewModel?
  • How do I wire up my View and ViewModels at run-time and design-time?
  • How does a View and ViewModel communicate with one another?
  • Should my ViewModel talk directly to Entity Framework or other data technologies?

M-V-VM Part 3 –Application Services

This 60 minute presentation will walk you through universal application requirements and how the M-V-VM pattern provides a simple solution to these questions:

  • How can a ViewModel initiate opening of a dialog and get a response?
  • How can a ViewModel log exceptions?
  • How can a ViewModel communicate to another ViewModel or View?
  • How can a ViewModel expose design-time sample data?
  • How can I resolve ViewModel dependencies?

M-V-VM Part 4 – Tool-able Applications

Tool-ability or Blend-ability is getting a lot of attention in the WPF, Silverlight and Windows Phone 7 space. At the end of 30 minute session you will understand:

  • Solutions to common coding problems that limit the tool-ability of an application
  • Techniques for surfacing design-time sample data

M-V-VM Part 5 – Testing M-V-VM Applications

One of the motivations for using the M-V-VM pattern is to be able to easily test the application logic. This 30 minute session will cover unit and integration testing in M-V-VM applications. At the end of this session you will be able to answer:

  • How can I test a ViewModel in isolation and why should I care?
  • What tools are available to help me write tests and run them?

Close

It has been just over two years since I moved from Charlotte to Redmond.  I’m very much looking forward to reconnecting with my friends in North and South Carolina.

Have a great day,

Just a grain of sand on the worlds beaches.


Follow

Get every new post delivered to your Inbox.

Join 136 other followers