Search code examples
c#castle-windsorioc-container

Is it possible in Castle Windsor to make a service with dependency on property of another component?


Let's say I have ISettings with a property string Setting1 and I have

public class MyComponent : IMyService
{
    public MyComponent(string setting1)
    {
        // set fields
    }
}

Is it possible to wire up Windsor to say that ISettings.Setting1 should be used to satisfy the dependency of MyComponent?


Solution

  • I would suggest 2 options.

    First, use ISettings as dependency and use Setting1 where necessary

    public class MyComponent : IMyService
    {
        public MyComponent(ISettings settings)
        {
            // access settings.Setting1
        }
    }
    

    Second, Windsor .DependsOn function to pass some primitive properties to component.

    container.Register(
                    Component.For<IMyService >()
                    .ImplementedBy<MyComponent >()
                    .DependsOn(Dependency.OnValue("setting1", ISettingsInstance.Setting1));