castle-windsor Fluent API Registration DependsOn - Specify Dependencies


Example

The entire idea with dependency injection is that a class does not instantiate its dependencies but requests them (through constructor or property). Using Castle the way for specifying the way to resolve a dependency is by using the DependsOn:

public class Foo : IFoo
{
    public Foo(IBar bar, string val)
    {
        Bar = bar;
        Val = val;
    }
    public IBar Bar { get; set; }
    public string Val { get; set; }
}

container.Register(
    Component.For<IBar>().ImplementedBy<Bar>().Named("bar1"),
    Component.For<IBar>().ImplementedBy<Bar>().Named("bar2"),

    Component.For<IFoo>()
             .ImplementedBy<Foo>()
             .DependsOn(Dependency.OnComponent("bar", "bar1"),
                        Dependency.OnValue("val","some value")));