3

I need help with configuring MS Unity.

I have a class implementing an interface:

public class ProjectService : IProjectService

which works fine with this configuration:

_conainer.RegisterType<IProjectService, ProjectService>();

I another, caching, implementation, I need the first concrete type injected into the caching concrete type.

public class CachedProjectService : IProjectService
{
    public CachedProjectService(IProjectService projectService, ICacheStorage cacheStorage)
    {}
}

How can I configure Unity to return the caching version with the first implementation injected into it?

1 Answers1

3

It's called decorators wiring that you can achieve like this :

_container.RegisterType<IProjectService, ProjectService>("innerService");

_container.RegisterType<IProjectService, CachedProjectService>(
    new InjectionConstructor(
        new ResolvedParameter<IProjectService>("innerService"), 
        new ResolvedParameter<ICacheStorage>()
    ));

Hope it helps

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73