Search code examples
c#dependency-injectioncastle-windsor

How to pass properties from other registered components in castle Windsor


I have to get the value of property that is part of component already registered in my DI Container and use it in constructor of other component that will be registered.

For example:

container.Register(Component.For<IFoo>().ImplementedBy<Foo>());

container.Register(Component.For<Da>().DependsOn(
    /*Here I want to somehow pass from Foo object property FooConnection*/
    );

I was thinking about creating an constructor that would get IFoo object but in that way I will introduce circular dependency (IFoo/Foo and Da are in other assemblies) and it won't work.

EDIT:

So far the only solution I can think of is this, which is far from ideal in my opinion:

container.Register(Component.For<Da>().DependsOn(
    Dependency.OnValue<string>(container.Resolve<IFoo>().FooConnection)));

Solution

  • Create a third class Configuration which the others will depend on and put it to the assembly which both are referencing.

    container.Register(Component.For<Configuration>())
    
    public class Configuration
    {
        //your properties
    }
    
    public class Foo
    {
        public Foo(Configuration configuration)
        {
        }
    }
    
    public class Da
    {
        public Da(Configuration configuration)
        {
        }
    }