jeudi 6 septembre 2018

Passing requesting class type to a requested class constructor

Let's say I have a following class:

public class Handler
{
    public Type RequesterType { get; }

    public Handler(Type requesterType)
    {
        RequesterType = requesterType;
    }
}

which is itself a dependency of another class:

public class Controller
{
    public Handler Handler { get; }

    public Controller(Handler handler)
    {
        Handler = handler;
    }
}


Is it possible to register Handler in such a way that when Controller is being resolved, the dependent Handler's constructor argument requesterType is being assigned with Controller type?

Here's an example of what I want to achieve:

class Program
{
    static void Main(string[] args)
    {
        var container = new WindsorContainer();
        container.Register(
            Component.For<Controller>()
            //Component.For<Handler>(), <- this is the essense of my question
        );

        var controller = container.Resolve<Controller>();
        Console.WriteLine(controller.Handler.RequesterType); //Should output Program.Controller
        Console.ReadKey();
    }
}





Aucun commentaire:

Enregistrer un commentaire