Search code examples
c#dependency-injectioncastle-windsor

Castle resolving in deep hierarchy


I have a problem with configuring our castle windsor ioc container to do what I want.

I have the following structure:

SessionInfo : ISessionInfo

ConnectionInfo(ISessionInfo sessionInfo);
Repository(IConnectionInfo connectionInfo);

All these classes are instantiated by castle. This is the default case. Now I need to modify the SessionInfo from the outside. To accomplish this I implemented an StaticSessionInfo. Now I have exactly one case, where the ConnectionInfo object should not be a SessionInfo object but a StaticSessionInfo object.

Unfortunately I don't have access to the code that calls the Resolve parts. I just can configure the castle windsor container and at the end get the Repository.

I think I need the ability to do some register like the following but I cant get it working:

windsorContainer
 .Register(Component.For<IRepository>()
                    .ImplementedBy<Repository>()
                    .Named("DynamicRepository")
                    .DependsOn(Dependency.OnComponent<ISessionInfo, IStaticSessionInfo>()));

The part that is not working is the DependsOn part. Am I on the right path or do I misunderstand something?


Solution

  • You need to specify the implementation type, not the interface, as the second type parameter to OnComponent.