Click here to Skip to main content
Click here to Skip to main content

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

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 :

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 !

License

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

About the Author

Houssem_Dellai
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
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 19 Sep 2013
Article Copyright 2013 by Houssem_Dellai
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid