Search code examples
c#castle-windsorfactoryioc-container

Type not resolving in multiple factories


I have following class

public class NightlyJob : IScheduleJob, IRecurringJob

For both interfaces I have simple typed factories like this:

public interface IScheduleJobFactory
{
    IEnumerable<IScheduleJob> Create();
}

Mentioned class is not retrieved from both factories' .Create() methods. It seems that the class can be retrieved from only one factory depending on which of the interfaces in the signature is written first.

My question is if there is a way around this?


Solution

  • After some searching in the direction suggested by Steven in comments. I found my solution to be injecting IEnumerable<IScheduleJob> and IEnumerable<IRecurringJob> into the consumers directly. These are resolved as collections of implementations by using Castle's CollectionResolver like this:

    container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel));
            container.Register(
                Classes.FromAssemblyInDirectory(new AssemblyFilter("."))
                    .BasedOn(typeof(IScheduleJob)).
                    WithServiceAllInterfaces()
            );