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
an abstraction over IoC containers and service locators. This shared interface supports adapters for:
- Castle Windsor
- Spring .NET
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:
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
In this case, a new class
SMViewLocator will be created, then delete
ViewModelLocator. The new class is defined like this:
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
IServiceLocator interface. This is the common shared interface in the Common Service Locator library
to query the container.
The code for the function is:
private static IServiceLocator CreateServiceLocator()
Registry registry = new Registry();
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:
registers all classes in the executing assembly; with the built-in default container, each class must be registered like this:
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:
var ioc = CreateServiceLocator();
ServiceLocator.SetLocatorProvider(() => ioc);
For every model in the solution, there will be a model accessor like this:
public MainViewModel Main
And finally there is a cleanup function to execute when the main window is closed.
public static void Cleanup()
In order to use the new model locator, it just takes to change the reference in the app.config file:
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
<vm:SMViewModelLocator x:Key="Locator" d:IsDataSource="True" />
And in the main window, change this piece of code:
Closing += (s, e) => ViewModelLocator.Cleanup();
into this one:
Closing += (s, e) => SMViewModelLocator.Cleanup();
The objective was to be able to change the container, the behavior is the same:
With the new support for the Common Service Locator library, changing the IoC container
in an MVVM Light implementation is pretty easy.