Reputation: 2548
My task is to inject specific non-direct child dependency if a top level dependency is of some type. I've tried achieving this with subcontainers, but it does not work as I expected.
I have a next class hierarchy:
PRMController: ApiController
-> PManager: IManager
-> Repository: IRepository
-> RepositoryConnection: IRepositoryConnection
RepositoryConnection() { /*use default config name*/ }
RepositoryConnection(string configName)
The windsor container has next registrations:
container.Register(Classes.FromAssemblyInThisApplication()
Pick()
.Configure(c =>
{
c.Named(Guid.NewGuid().ToString());
c.IsFallback();
})
.WithService.DefaultInterfaces()
.LifestyleTransient()
);
var childContainer = new WindsorContainer()
.Register(
Component.For<IRepositoryConnection>().ImplementedBy<RepositoryConnection>()
.DependsOn(Dependency.OnValue("configName", "PRM"))
.LifestyleTransient()
);
container.AddChildContainer(childContainer);
Now for PRM controller it should use the "PRM" configName. And therefore the child container is used to resolve the PRM controller. But the RepositoryConnection
is resolved with a default config.
childContainer.Resolve<IRepositoryConnection>(); //as expected, resolves from child container registrations
childContainer.Resolve<PRMController>(); //NOT as expected, the underlying repositoryConnection is from parent container
Can someone please shed a light on that? I thought windsor would prefer child container dependencies if they exist even if resolving from within child container for a type registered in a parent container. If that is not achievable, I would be grateful if someone could lead me in the right direction.
Update: According to to this thread there is a bug. So is it going to be resolved? What other strategy for the described scenario could I take?
Upvotes: 1
Views: 466
Reputation: 2548
Have achieved what I wanted through the IHandleSelector, so in HasOpinion
do a check for the service and whether current Url is for PRMController
. SelectHandler
returns the required type of IRepositoryConnection
.
I would better stick with the child container, as at the controller factory I know exactly which controller it is, while with handle selector I need to parse it from Request.Url.
Unfortunately the child container route seems to be not behaving as expected (at least as I expected) in Windsor 3.2 and 3.3.
Upvotes: 1