Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing part of constructor parameters to castle windsor container

I have constructor

Foo(IColor c , int someNumber)

and I know the some number only during the run time, and I want to call this constructor during the resolving and to pass someNumber value and the IColor to be resolved autocratically.

Something like this:

container.Resolve<IFoo>(someNumber);

Is it possible to be done ?

like image 260
Night Walker Avatar asked Jul 30 '12 17:07

Night Walker


1 Answers

You should prefer Typed Factory instead of using container like service locator. Just define factory interface:

public interface IFooFactory {
    IFoo Create(int somenumber);
}

and register it as typed factory:

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

Then use dependency injection to inject factory and use it:

var foo = fooFactory.Create(desiredArgumentValue);

For more info read Windsor documentation

like image 128
Aleš Roubíček Avatar answered Nov 02 '22 16:11

Aleš Roubíček