Testable Navigation with MVVM Fabric

One of the biggest problems I encountered while writing WPF apps with MVVM is navigation.  The traditional approach to navigation would be to have one view create the view being navigated to.  Now, there’s a couple of issues with that approach which make it less than desirable.  The first issue is that having one view create another view heavily couples the two views.  The second issue is that this approach quickly becomes very difficult (if not impossible) to unit test.

Things get more complicated when view models are introduced.  Since the functionality that initiates the navigation is going to be in the view model, that would mean the view model would have to know about the new view.  When I structure an MVVM solution for WPF, I create a separate class library for the view models.  The client project (which contains the views) needs to have a reference to the view models project in order to use them.  That means there would be a circular dependency if the view model project had a reference to the view project as well.  Doing the traditional approach with this project structure is no longer even an option.

MVVM Fabric solves the navigation problem with some convention, some configuration, a pinch of dependency injection and a dash of the message bus.

10,000 ft View

MVVM Fabric funnels all navigation through one source, the ViewController.  The ViewController subscribes to the message bus to listen for ShowViewMessages requesting views.  When a view request comes through, the ViewController coordinates the resolution and creation of the view and its view model.  Then the ViewController takes the new view and passes it along to the view placer to be placed in the application.

By leaning on the message bus to request a view, writing unit tests for the view model making the request becomes a breeze.  By letting all navigation flow through the ViewController, it de-clutters your codebase by eliminating the need for code creating views throughout the application.  This decoupling will greatly simplify your application and each view model can focus on its own responsibilities and not have to worry about the specifics of any other view or view model.

Convention

As I mentioned, there is an element of convention required to leverage the navigation model provided by MVVM Fabric.  The conventions are as follows:

  1. All view models must have a default constructor.
  2. View models have the option of providing a Load method as a way of allowing itself to be loaded during the dynamic creation process provided by MVVM Fabric.  The Load method may take 0 or 1 arguments.
    1. If there is an argument, it can be whatever is required by the view model to load itself.
    2. If more than one thing is required to load the view model, I recommend creating some sort of criteria class that contains any required data.
  3. A class inheriting from ViewTargets must be provided with unique definitions for all navigable views in the application.

The reason that all view models must have a default constructor is that they are dynamically instantiated when a view is loaded.  Since there are many scenarios where a view model will need some information to load itself (picture a detail view, for example), MVVM Fabric looks for a public Load method on the view model and calls it if found.  That Load method may be a no argument method which simply kick-starts the loading needed for the view model or it may take a single parameter.  If the Load method takes a parameter, a parameter must be provided when the view is requested.  That’s where the ShowViewMessage comes into play.

The ShowViewMessage has a ViewTargets parameter (which tells the ViewController which view is being requested) and an optional parameter called LoadArgs.  LoadArgs can be anything required by the view model, but if something is provided a Load method which takes that object type as a parameter must exist on the view model or an exception will be thrown.

If you saw my presentation WPF with MVVM: From the Trenches, you heard me talking about how ViewTargets is an enumeration.  While refactoring my navigation code into a stand-alone library for MVVM Fabric, one of the issues I had to overcome is the fact that enumerations cannot be inherited from and extended from an external object.  The reason I liked using an enumeration to articulate all the navigable views in the first place is because it provided a level of safety at compile time when views are requested.  I could have used strings instead to identify views, but that would not make refactoring easy.  To overcome this limitation of enums, I leveraged a concept introduced by Hugh Ang to create an Enumeration class which acts like an enum, but can be extended.  It’s not a perfect solution, but it does help address the compile-time and refactoring issues.  The new ViewTargets inherits from this Enumeration class and must be further inherited from in your implementation.

Configuration

In order to leverage the navigation model provided by MVVM Fabric, you must provide implementations to an interface or two: IViewPlacer and optionally IViewAuthorizer.

IViewPlacer is what will actually place the views when the navigation model has loaded the view and it’s view model.  I leave that implementation up to you because each application is different in how views are presented.  The sample application provides an implementation that works with the tabbed interface used by Movies.  MVVM Fabric does contain an implementation of IViewPlacer for views displayed as modal dialogs because this will likely be the same across applications.  The sample application shows the usage of ModalViewPlacer in conjunction with it’s custom IViewPlacer to illustrate how you could implement a combination of the two.

IViewAuthorizer is an interface which can optionally be implemented to give you control over whether the user is authorized to see each view before it is shown.  This allows you to put restrictions on your views from one location.

Enough talk, time for code!

Here’s the part you are probably waiting for, how do I make it work?  Here are the steps that you need to take to get up and running.

1.  Inherit from ViewTargets and provide values for your views.

2.   Provide an implementation for IViewPlacer (unless you strictly use ModalViewPlacer)

Note: Your custom ViewPlacer would also be a good place to subscribe to CloseViewMessages from the message bus.  Since views have the ability to request that they be closed, it only makes sense to keep the showing and closing functionality in the same place.  You can see this in action with the sample application.

3.  Provide a way to wire up the IViewConfigurationResolver when the application starts up.

Note: The sample application calls ContainerConfiguration.InitContainer() in the application’s OnStartup method.  I also do some configuration for Windsor Container in this method.

4 (Optional).  Provide an implementation for IViewAuthorizer.  This one is up to you since application security varies on a case-by-case basis.

5.  Instantiate the ViewController and all the pieces it needs.  This will probably be done in the code-behind of your main window.  The sample application uses Castle Windsor, so I will leave it up to you how you get all the pieces standing.

You’re There!

The steps described above may seem like a lot of work, but the work is almost all front-loaded.  When a new view is added, only steps 1 and 3 must be revised to accommodate the new view.

Now, let’s look at how to initiate navigation.

That’s it!  This example shows how to pass a parameter to the Detail view.  You could remove the LoadArgs parameter from the ShowViewMessage for those cases where there is either no need for a Load method on the view model or the Load method has no parameters.

From a testing perspective, the above example is extremely simple to write unit tests for.  You would simply assert that the message bus received a ShowViewMessage with the correct arguments.

Conclusion

The navigation model provided by MVVM Fabric takes a pain point with the MVVM pattern and smooths it over in a testable manner.  With all the navigation being handled in one place, it reduces the surface area that you must test for in your application.  A testable application is a maintainable application!

Share

2 thoughts on “Testable Navigation with MVVM Fabric”

Comments are closed.