Kona – The ViewModelLocator Class

January 26, 2013

no comments

In previous posts I mentioned how, by using the Binding markup extension, the view automatically resolves its bounded properties from a view model object that is stored in its DataContext property. How the view model ends up being referenced in the view’s DataContext property was left to be some kind of magic. In this post I will talk about the component that is responsible for making this happen – the ViewModelLocator.

Before diving into the details, let’s re-set the context. We have a simple view model with a single property, named FirstName:

   1: using Kona.Infrastructure;

   2:  

   3: namespace KonaSample

   4: {

   5:     public class MainPageViewModel : ViewModel

   6:     {

   7:         private string m_firstName;

   8:  

   9:         public MainPageViewModel()

  10:         {

  11:             FirstName = "Moshe";

  12:         }

  13:  

  14:         public string FirstName

  15:         {

  16:             get { return m_firstName; }

  17:             set { SetProperty(ref m_firstName, value); }

  18:         }

  19:     }

  20: }

And a single page containing a TextBlock control with its Text property bounded to the view model’s FirstName property:

   1: <Page

   2:     x:Class="KonaSample.MainPage"

   3:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   4:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

   5:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

   6:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

   7:     xmlns:Infrastructure="using:Kona.Infrastructure" 

   8:     Infrastructure:ViewModelLocator.AutoWireViewModel="True"

   9:     mc:Ignorable="d">

  10:  

  11:     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

  12:         <TextBlock Text="{Binding FirstName}"

  13:                    VerticalAlignment="Center"

  14:                    HorizontalAlignment="Center"/>

  15:     </Grid>

  16: </Page>

Notice how the AutoWireViewModel attached property is set on the Page control. This property is where the magic happens. The ViewModelLocator is implemented as an attached behavior. All you have to do is set the AutoWireViewModel attached property on the page, and the ViewModelLocator chooses the appropriate view model, creates it, and stores its reference in the page’s DataContext property.

There are two different ways for the ViewModelLocator to choose the right view model for a view: Conventions and Factories.

Using Conventions

In this approach, the view model is chosen based on a convention, which is by default, a class in the same assembly and with the same full name as the view, with the ViewModel suffix. For example, for the above view, named KonaSample.MainPage, the KonaSample.MainPageViewModel class, contained in the same assembly, is chosen by default.

The default convention can be overridden with the SetDefaultViewTypeToViewModelTypeResolver method, which receives a delegate that maps between the view type and the matching view model type. For example, to look for the view models in their own namespace, use the following convention:

   1: ViewModelLocator.SetDefaultViewTypeToViewModelTypeResolver(viewType =>

   2:             {

   3:                 string viewModelName = String.Format("KonaSample.ViewModels.{0}ViewModel, {1}", 

   4:                                                      viewType.Name, 

   5:                                                      viewType.GetTypeInfo().Assembly.FullName);

   6:                 return Type.GetType(viewModelName);

   7:             });

Once the appropriate view model type is found, a new instance is created by calling the Activator.CreateInstance method. This default instantiation behavior is not enough for view models without a parameter-less constructor, or when the same view model instance should be used every time. It can be overridden with the SetDefaultViewModelFactory method, which receives a delegate that returns a ViewModel instance based on a given view model Type. For example, the following code uses an IoC container to resolve view models:

   1: ViewModelLocator.SetDefaultViewModelFactory(viewModelType => m_container.Resolve(viewModelType) as ViewModel);

Using Custom Factories

A different, more verbose approach, than using conventions, is to register a specific view model factory for each view. Factories are in higher priority than conventions. The ViewModelLocator falls back to using conventions only after it fails to resolve a view model with a custom factory. Factories are registered per view type. For example, the following code registers a custom factory for the above view model:

   1: ViewModelLocator.Register(typeof(MainPage), () => new MainPageViewModel());

Unlike the SetDefaultViewModelFactory method, which receives a delegate that returns a ViewModel derived class, the Register method receives a delegate that returns a BindableBase derived class, which is less restrictive. I assume that it will change in the official release and both methods will return BindableBase derived objects, as currently, there is no reason to restrict the view models to derive from the ViewModel class.

Another issue, that I assume will be fixed in the official release, is that, currently, there is no Unregister method to remove existing factories.

I prefer to use conventions whenever possible and register a specific factory only in special cases. That way, I don’t need to update the registration code for each new view.

Cross-posted from http://www.programmingtidbits.com/post/2013/01/26/Kona-The-ViewModelLocator-Class.aspx

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*