Click here to Skip to main content
15,883,603 members
Articles / Programming Languages / XML
Article

Implementing MVVM Light with Structure Map

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
9 Dec 2011CPOL3 min read 34.7K   763   6   2
This article is about implementing an MVVM Light based solution with an IoC container other than the built-in SimpleIoC container included in the MVVM Light framework.

Introduction

This article is about implementing an MVVM Light based solution with an IoC container other than the built-in SimpleIoC container included in the MVVM Light framework.

The MVVM Light 4.0 release implements a built-in IoC container and adapter supporting the Common Service Locator library, leaving the developer free to use whatever IoC container or service locator with the proper adapter, but there is not a demo showing how to do it, this article pretends to show how to do it in the simplest way.

Building the Code

The first step is to chose the IoC container, in this article, MVVM Light will be implemented with a Structure Map IoC container. Why this one? There is not a specific reason to use this container, it was just picked randomly, the only criteria for this article was the IoC container must support a Fluent configuration interface. The StructureMap and the adapter binaries are included in the source code.

The Common Service Locator library contains a shared interface IServiceLocator supporting an abstraction over IoC containers and service locators. This shared interface supports adapters for:

  • Castle Windsor
  • Spring .NET
  • Unity
  • StructureMap
  • Autofac
  • MEF
  • LinFu

Then it is necessary to create a new MVVM Light project named "DemoStructureMapMVVMLight" (in this article, the chosen template was MVVMLight(WPF4)), and add a new folder named "Lib" containing the files:

  • StructureMap.dll
  • StructureMapAdapter.dll

Add a reference to these files. By default, the framework profile is ".NET Framework 4.0 Client", change the profile to ".NET Framework 4.0".

Your project tree must look like this:

There is at least a couple of ways to insert the new IoC container in MVVM Light, the simplest way is to modify the ViewModelLocator. In this case, a new class SMViewLocator will be created, then delete ViewModelLocator. The new class is defined like this:

C#
using DemoStructureMap.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using StructureMap;
using StructureMap.Configuration.DSL;
using StructureMap.ServiceLocatorAdapter;
using System.Reflection;

namespace DemoStructureMap.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class SMViewModelLocator
    {
    }
}

The first thing to do is to configure the container, this logic depends on the implemented container; in the case of a container like Unity, the configuration is done by an XML file, in this case, the configuration is done by code. The configuration function will return an instance of the IServiceLocator interface. This is the common shared interface in the Common Service Locator library to query the container.

The code for the function is:

C#
private static  IServiceLocator CreateServiceLocator()
{
    Registry registry = new Registry();

    registry.ScanAssemblies().IncludeAssembly(
             Assembly.GetExecutingAssembly().FullName);

    if (ViewModelBase.IsInDesignModeStatic)
    {
        registry.ForRequestedType<IDataService>().
          TheDefaultIsConcreteType<Design.DesignDataService>();
    }
    else
    {
        registry.ForRequestedType<IDataService>().
          TheDefaultIsConcreteType<DataService>();
    }

    IContainer container = new Container(registry);

    return new StructureMapServiceLocator(container);
}

With Structure Map, there is no need to register each class in the project, the line:

C#
registry.ScanAssemblies().IncludeAssembly(Assembly.GetExecutingAssembly().FullName);

registers all classes in the executing assembly; with the built-in default container, each class must be registered like this:

C#
SimpleIoc.Default.Register<MainViewModel>();

Not so easy if there is a lot of models to register!!!

A new static constructor is needed to assign the newly created interface instance to the service locator:

C#
static SMViewModelLocator()
{
    var ioc = CreateServiceLocator();
    ServiceLocator.SetLocatorProvider(() => ioc);
}

For every model in the solution, there will be a model accessor like this:

C#
public MainViewModel Main
{
    get
    {
        return ServiceLocator.Current.GetInstance<MainViewModel>();
    }
}

And finally there is a cleanup function to execute when the main window is closed.

C#
public static void Cleanup()
{
    //Clean up code
}

In order to use the new model locator, it just takes to change the reference in the app.config file:

XML
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

into this:

XML
<vm:SMViewModelLocator x:Key="Locator" d:IsDataSource="True" />

And in the main window, change this piece of code:

C#
public MainWindow()
{
    InitializeComponent();
    Closing += (s, e) => ViewModelLocator.Cleanup();
}

into this one:

C#
public MainWindow()
{
    InitializeComponent();
    Closing += (s, e) => SMViewModelLocator.Cleanup();
}

The objective was to be able to change the container, the behavior is the same:

Conclusion

With the new support for the Common Service Locator library, changing the IoC container in an MVVM Light implementation is pretty easy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Fujitsu Canada
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMessageBox service Pin
George Swan9-Dec-11 22:58
mveGeorge Swan9-Dec-11 22:58 
AnswerRe: MessageBox service Pin
emardini12-Dec-11 3:47
emardini12-Dec-11 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.