Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to UsingFactoryMethod in Castle Windsor

How do I pass dynamic parameters to a UsingFactoryMethod registration?

For example, I want to write something like:

container.Register(
   Component.For<IFoo>()
        .UsingFactoryMethod(return DoSomethingAndReturnInstance(paremeter)));

I need the parameters to be sent at runtime, like this:

container.Resolve<IFoo>(new { parameter = value });

How can it be done?

like image 209
Ilya Kogan Avatar asked Oct 21 '25 02:10

Ilya Kogan


2 Answers

CreationContext.AdditionalParameters has the values you pass to Resolve

like image 75
Krzysztof Kozmic Avatar answered Oct 24 '25 12:10

Krzysztof Kozmic


You just have to use

container.Register(
    Component.For<IFoo>()
        .UsingFactoryMethod((kernel, creationContext) =>
        {
            var parameter = creationContext.AdditionalArguments["parameter"];
            return DoSomethingAndReturnInstance(parameter);
        }));
like image 35
Vadim Erdakov Avatar answered Oct 24 '25 12:10

Vadim Erdakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!