0
votes

I have 2 interfaces, one inherits from the other. For the base interface I have a singleton implementation, and for the sub interface I have a decorator singleton implementation (that decorates the base implementation)

Now if the base interface is injected (or resolved) into other clients, they get the base implementation, not the decorator (they only get the decorator if they depend on the sub interface)

What I want is, the decorator implementation should get injected into each client, but the base implementation still needs to be constructed and injected into the decorator singleton.

How do I set this up ?

using System;
using Microsoft.Practices.Unity;

public interface IProvider
{
    int GetValue();
}

public interface ITracker : IProvider
{       
}

public class Provider : IProvider
{
    public int GetValue()
    {
         Console.WriteLine("provider");
         return 0;
    }
}

Tracker:

public class Tracker : ITracker
{
    private readonly IProvider provider;

    public Tracker(IProvider provider)
    {
         this.provider = provider;
    }

    public int GetValue()
    {
        Console.WriteLine("tracker");
        return provider.GetValue();
    }
}

Program:

class Program
{
    static void Main(string[] args)
    {
        var container = new UnityContainer();

        container.RegisterType<IProvider, Provider>(
            new ContainerControlledLifetimeManager());
        container.RegisterType<ITracker, Tracker>(
            new ContainerControlledLifetimeManager());

        // want to get Tracker singleton instance here 
        var provider = container.Resolve<IProvider>(); 

        // want to get Tracker singleton instance here
        var tracker = container.Resolve<ITracker>(); 
    }
}
1
You want all instances of ITracker to have the same instance of IProvider? - robjam
there will only be one instance of provider and one instance of tracker (both registered as singletons) the tracker will get the provider as a dependency. but clients should get the tracker singleton, no matter if they depend on IProvider or ITracker - user1803928
So you only want to the IProvider to be injected into the Tracker implementation. If IProvider is a dependency on any other class it should get ITracker? - Scrobi
@Scrobi: exactly. the decorator is the only component that should get the provider, all other should get the decorator. - user1803928

1 Answers

0
votes

I am no expert in Unity but this is what I am up with:

As the Tracker is a singleton you can pass in the IProvider as a constructor parameter. You then set up both IProvider & ITracker to resolve to as instance of Tracker. Though I still had to cast the IProvder to ITracker.

var container = new UnityContainer();
container.RegisterType<IDependency, Dependency>();   
container.RegisterType<IProvider, Provider>("Provider");


container.RegisterType<ITracker, Tracker>
(
    new ContainerControlledLifetimeManager(),
      new InjectionConstructor(
        new ResolvedParameter<IProvider>("Provider")
));

container.RegisterType<IProvider, Tracker>
(
    new ContainerControlledLifetimeManager(),
      new InjectionConstructor(
        new ResolvedParameter<IProvider>("Provider")
));

var p = container.Resolve<IProvider>() as ITracker;
var t = container.Resolve<ITracker>();