Andre
Andre

Reputation: 1044

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?

Upvotes: 0

Views: 134

Answers (2)

Phil Degenhardt
Phil Degenhardt

Reputation: 7264

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

Upvotes: 1

Andre
Andre

Reputation: 1044

Fortunately a colleague of mine has a way better Idea to solve the problem and I implemented that way. While doing that I got the opinion that my solution I posted here is not good in general.

Why?

How did I get the idea to do it this way? I saw the implementation of the IConnectionInfo class that was used for my special case. I saw that it has a dependency to the ISessionInfo interface. If I dont had the possibility to see the implementation, I had never seen that dependecy. Because of this I think the solution I wanted to implement first is not only not good but a bad one.

Upvotes: 2

Related Questions