I have the following problem. I have two classes T1 and T2 that both implement interface T. I'd like T2 to decorate T1 so when I create an instance of T T2 is called first, then T1.
public class T1 : IT
{
public void Call()
{
Write("T1");
}
}
public class T2 : IT
{
private IT _t;
public T2(ISomeOther other, IT t)
{
_t = t;
}
public void Call()
{
Write("T2");
_t.Call();
}
}
public interface T {
void Call();
}
My unity looks like this:
var unityContainer = new UnityContainer();
unityContainer.RegisterType<ISomeOther>(new Other());
unityContainer.RegisterType<T>(new T1());
unityContainer.RegisterType<T, T2>(new InjectionConstructor(new ResolvedParameter<ISomeOther>(), new ResolvedParameter<T2>());
I'd like to avoid having to explicitly spell out ResolvedParameters - the whole purpose in Unity is to remove this sort of work. I'd like to simply tell Unity that T2 decorates T1, and have Unity resolve the arguments. I'd have liked my final line of code to like to look something like this:
unityContainer.RegisterType<T, T2>().Decorates<T1>();
Is such a thing possible in Unity? Are there other IoC containers that are better for this sort of thing?
Note this is a simplified example so any minor syntax errors are incidental.
Edit:
This is not a duplicate of How do I use the Decorator Pattern with Unity without explicitly specifying every parameter in the InjectionConstructor - I DO NOT WANT TO EXPLICITLY CREATE THE DECORATOR OR LIST OUT THE DEPENDENCIES. Like this:
_container.Register<IT, T1>();
_container.Decorate<IT, T2>();
_container.Decorate<IT, T3>();
Where T1, T2 and T3 may have various dependencies in the constructor - I don't want to list these out, I want the IoC container to resolve these for me (otherwise why use an IoC container at all?)