Darf Zon
Darf Zon

Reputation: 6378

MS UNITY: How to access to all my modules a dictionary?

I'm using MS Unity Container. And all my modules needs to use a common object where I add the values from a dictionary like this:

Dictionary<Type,  Func<BaseItem, BaseItemViewModel>> Maps

And when I use a viewModel, this one receives the common Dictionary.

A partner told me something about configure the Unity, but my knowledge about Unity is few.

Upvotes: 0

Views: 137

Answers (1)

Richard Friend
Richard Friend

Reputation: 16018

Create a service and register it with unity.

For example

public interface ICommonDataService
{
     Dictionary<Type,Func<BaseItem, BaseItemViewModel>> GetMaps();
}

public class CommonDataService : ICommonDataService
{
    public Dictionary<Type,Func<BaseItem, BaseItemViewModel>> GetMaps()
    {
         //Implementation
    }
}

Then register with unity in your bootstrapper

protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        Container.RegisterType<ICommonDataService, CommonDataService>(new ContainerControlledLifetimeManager());
}

Then you can have this injected or use ServiceLocator to resolve the instance from the interface...

Upvotes: 1

Related Questions