Lodewijk
Lodewijk

Reputation: 2401

Castle Windsor Typed Factory Close Open Generic

I'm not sure if this is possible, but we want to do the following

We have Processors that processes a type of Message. Each Message has a MessageOrigin. The Processor need diffrent Mappers based on the MessageOrigin. The Processor is not interested in the MessageOrigin.

This gives us the following code (full Gist here)

public class ConcreteMessageProcessor<TMessageOrigin> 
    : IProcessor<ConcreteMessage, TMessageOrigin>
{
    public ConcreteMessageProcessor(IMapper<TMessageOrigin> mapper){}
}

We also have a TypedFactory to create the processor:

public interface IProcessorFactory
{
    IProcessor[] GetAllProcessorsForMessage(Message message, 
        IMessageOrigin origin);
}

Combined with a Selector:

public class ProcessorSelector : DefaultTypedFactoryComponentSelector
{
    protected override Type GetComponentType(MethodInfo method, 
        object[] arguments)
    {
        return typeof(IProcessor<,>).MakeGenericType(arguments[0].GetType(),
            arguments[1].GetType()).MakeArrayType();
    }
}

However, when we call the IProcessorFactory, we never get a processor. I guess that this is because the TMessageOrigin is still open when the type is registered in the container. How can I resolve this?

Upvotes: 1

Views: 330

Answers (1)

samy
samy

Reputation: 14972

From what i see in your gist you don't have an implementation of a ProcessorImp that specifies both its generic types. That's certainly the reason why the factory cannot resolve IProcessor: you're trying to resolve the service IProcessor<MessageImpl, MessageOriginImp> but the component that you have registered implements

IProcessor<Message, TMessageOrigin>
where TMessageOrigin : IMessageOrigin

Perhaps you should rethink the way you are trying to register/resolve your services; your MessageOriginImpl could have an interface IIsOriginFor<MessageImpl> and the factory would resolve these based on the type of MessageImpl

Upvotes: 2

Related Questions