Unity Context like RegionContext?

Topics: Prism v1, Prism v2 - Silverlight 2, Prism v2 - Silverlight 3, Prism v2 - WPF 3.5
Feb 23, 2010 at 7:40 PM

Is there any way to get reference of the unity container as with Regions (RegionContext). Something like UnityContext.Resolve..., ?

Feb 26, 2010 at 6:05 PM

Hi,

One possible way to get a reference to the Unity container is getting it injected in the constructor of your class. All classes that implement the IModule interface are injected with dependencies, so in those kind of classes you simply have to add the Container’s interface to your constructor. In any other class you need to add the interface and also make sure it goes through the container’s builder pipeline (either by resolving it or calling the build up method).

Assuming you want something similar to the region context (share between parent and child views), you can make sure all the views get injected with the container.

You can check any of the Prism Quickstarts or the Reference Implementation to see how this is done.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Feb 26, 2010 at 6:15 PM

Hi

 

Actually I need to use unity container in one custom IValueConverter to, so instead of creating global instance of UnityContainer I wonder if there is same way to access it like with RegionContext.

 

 

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      if (value == null)
       throw new ArgumentNullException("value");
      IUnityContainer unityContainer = Module.MyContainer;

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)    {

 

      IUnityContainer unityContainer = Global.MyContainer; => HERE USE something like UnityContext instead?

}

 

Feb 26, 2010 at 6:47 PM

Hi,

You can add a reference to the Microsoft.Practices.ServiceLocation.dll (located in the Prism lib folder) and use the following code in your converter:

var myService = ServiceLocator.Current.GetInstance<WhatYouWantToResolveFromUnity>();

If you require the UnityContainer specifically (and not just getting a service) you can simply resolve the UnityContainer:

var container = ServiceLocator.Current.GetInstance<IUnityContainer>();

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Feb 26, 2010 at 8:44 PM

Yes!!

 

Both of those works, great! ServiceLocator was the one I was looking for!!!!!