Tekno
Tekno

Reputation: 193

How can I resolve dependency in Castle Windsor Factory?

I read about factories in CastleWindsor but I cannot get it clear. Hope anyone could help me. I have this typed factory in an MVC4 project.

public interface IOrderProcessorFactory
{
    T Create<T>(string ProcessorName) where T : IOrderProcessor;

    void Release(object service);

    IOrderProcessor GetTakeAway();
    IOrderProcessor GetInLocal();

}

this is register this way:

container.Register(Component.For<IOrderProcessorFactory>).AsFactory();   

container.Register(Component.For<IOrderProcessor>).ImplementedBy<TakeAwayOrderProcessor>().LifestylePerWebRequest().Named("TakeAway"));

container.Register(Component.For<IOrderProcessor>().ImplementedBy<InLocalOrderProcessor>().LifestylePerWebRequest().Named("InLocal"));

If inside an MVC controller I call the factory in this way.

_orderProcessorFactory.GetTakeAway();

I get the correct one, the one named "TakeAway". But for this I have to previous know the type. In other words, I want to call the factory get methods and pass a "name" and the factory returns the correct one.

For example in pseudo-code I want this

TakeAwayOrderProcessor processor1 = factory.GetMeProcessorCalled("TakeAway")
InLocalOrderProcessor processor2 = factory.GetMeProcessorCalled("InLocal")

I know I can pass parameters to the constructor but then I will have to select it "manually" with if name is this return this one else...

Is there any way Windsor can do this automatic, like StructureMap do with:

ObjectFactory.GetNamedInstance<IOrderProcessor>("InLocal");

Upvotes: 0

Views: 699

Answers (1)

Crixo
Crixo

Reputation: 3070

You need a TypedFactoryComponentSelector

Upvotes: 2

Related Questions