Search code examples
c#castle-windsordependency-resolver

Order when calling ResolveAll in Castle Windsor


Assume that I have multiple objects registered in the container, all implementing the same interface:

container.Register(
    Component.For<ITask>().ImplementedBy<Task1>(),
    Component.For<ITask>().ImplementedBy<Task2>(),
    Component.For<ITask>().ImplementedBy<Task3>(),
    Component.For<ITask>().ImplementedBy<Task4>(),
);

And I wish to resolve all the implementations of ITask:

var tasks = container.ResolveAll<ITask>();

Is there a way to control the order of the resolved instances?

Note: Obviously, I can implement an Order or Priority property on ITask, and just sort the list of tasks, but I am looking for a lower-level solution.


Solution

  • I believe that what you are looking for is a Handler Filter which based on the scarce documentation and the breaking changes in Windsor Castle 3; it provides filtering and sorting of components. Here's an extract from their Wiki page

    The interface is similar to IHandlerSelector but allow you to filter/sort handlers requested by container.ResolveAll() method

    So basically you will need to implement the IHandlerFilter interface and then add this implementation to the kernel while initializing the Windsor container. From the source code, this interface looks something similar to this...

    public interface IHandlerFilter
    {
        bool HasOpinionAbout(Type service);
        IHandler[] SelectHandlers(Type service, IHandler[] handlers);
    }
    

    Adding the Handler Filter to the kernel would be something like below...

     var container = new WindsorContainer()
            .Install(Configuration.FromXmlFile("components.config"));
            container.Kernel.AddHandlersFilter(new YourHandlerFilterImplementation());
    

    and then when resolving all components of a services with ResolveAll the order will be sorted (and filtered) based on your own implementation