Decorators in unity with multiple arguments

I have the following problem. I have two classes T1 and T2 that implement the interface T. I would like T2 to decorate T1, so when I instantiate 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 not to explicitly specify the ResolvedParameters - the whole goal in Unity is to remove this work. I would just like to tell Unity that T2 decorates T1 and Unity allows the arguments. I would love my last line of code to look something like this:

unityContainer.RegisterType<T, T2>().Decorates<T1>();

      

Is this possible in Oneness? Are there other IoC containers that are better suited for this kind of thing?

Note that this is a simplified example, so any minor syntax errors are accidental.

Edit:

This is not a duplicate How to use the Decorator pattern with Unity without explicitly specifying every parameter in the InjectionConstructor - I DON'T WANT EXCLUSIVELY CREATE A DECORATOR OR VIEW DEPENDENCE. Like this:

_container.Register<IT, T1>();
_container.Decorate<IT, T2>();
_container.Decorate<IT, T3>();

      

Where T1, T2 and T3 can have different dependencies in the constructor - I don't want to list them, I want the IoC container to resolve them for me (otherwise why use an IoC container at all?)

+3


source to share


1 answer


Ninject has some very useful features:

Bind<T>().To<T1>().WhenInjectedInto(typeof(T2));

      



It initializes T2 with an instance of T1 in its constructor. I hope I understood correctly. However, I'm really wondering if there is such a thing in Unity, since this is also my main IoC container.

+1


source







All Articles