Click here to Skip to main content
15,886,110 members
Articles / Game Development / Unity
Tip/Trick

Integrating Unity Container with MVVM Light Framework

Rate me:
Please Sign up or sign in to vote.
4.67/5 (4 votes)
18 Sep 2013CPOL 23.6K   7   1
Using Unity container instead of SimpleIoc in MVVM Light

Introduction

MVVM Light (mvvmlight.codeplex.com) 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 (unity.codeplex.com) or Windsor Castle (docs.castleproject.org/Windsor.MainPage.ashx). 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 :

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

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

C#
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.

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

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

That's it !

License

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


Written By
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 droidcon.tn/speakers.php.
http://houssemdellai.net

Comments and Discussions

 
QuestionGood solution Pin
vuongnguyen25-Aug-15 23:58
vuongnguyen25-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.