Click here to Skip to main content
13,148,573 members (56,606 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 18 Sep 2013

Integrating Unity Container with MVVM Light Framework

, 18 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Using Unity container instead of SimpleIoc in MVVM Light


MVVM Light ( is a very known and useful framework for building XAML based MVVM applications.

It provides its own IOC container implementation called SimpleIoc. SimpleIoc object is useful for simple use cases like registering and resolving instances. But, in some advanced scenarios, you want things as interception or advanced life-time management. In this case, using SimpleIoc is not enough. You need a more reach container such as Unity ( or Windsor Castle ( Fortunately, because MVVM Light uses ServiceLocation provided by Microsoft.Practices, it's possible to use another container.

Using the Code

In the following paragraphs, we'll see how to use Unity. The ServiceLocator object allows setting the container you want to use. It takes into parameter a delegate that should reference an object of type IServiceLocator. So, for the case of SimpleIoc, it's :

ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

In order to use Unity, we'll need to implement IServiceLocator.

public class UnityServiceLocator : IServiceLocator

    private readonly IUnityContainer _unityContainer; 
    public UnityServiceLocator(IUnityContainer unityContainer)
        _unityContainer = unityContainer;
    public object GetInstance(Type serviceType)
        return _unityContainer.Resolve(serviceType);
    public object GetInstance(Type serviceType, string key)
        return _unityContainer.Resolve(serviceType, key);
    public IEnumerable<object> GetAllInstances(Type serviceType)
        return _unityContainer.ResolveAll(serviceType);
    public TService GetInstance<TService>()
        return _unityContainer.Resolve<TService>();
    public TService GetInstance<TService>(string key)
        return _unityContainer.Resolve<TService>(key);
    public IEnumerable<TService> GetAllInstances<TService>()
        return _unityContainer.ResolveAll<TService>();

Then, we'll just set UnityServiceLocator to the SetLocatorProvider's parameter.

var unityContainer = new UnityContainer(); 
ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(unityContainer)); 

Finally, you can create your instances using UnityContainer instead of SimpleIoc.

That's it !


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


About the Author

Software Developer Microsoft
Tunisia Tunisia
I'm a Software Engineer and MVP (Client Development). I like writing articles and developing open source software in C#, Windows Phone and Windows 8 applications

You may also be interested in...


Comments and Discussions

QuestionGood solution Pin
vuongnguyen25-Aug-15 23:58
membervuongnguyen25-Aug-15 23:58 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170924.2 | Last Updated 19 Sep 2013
Article Copyright 2013 by Houssem_Dellai
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid