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>();
}
}