Castle Windsor: using convention registration with specific implementations

Suppose IFoo is implemented by Foo and IBar, implemented by FirstBar and SecondBar.

Using this registration registration:

container.Register(
    AllTypes.FromThisAssembly().Pick()
        .WithService.DefaultInterface())

We will have three entries in the container:

IFoo = Foo
IBar = FirstBar
IBar = SecondBar

Now, how can we configure this registration to tell the container that for IBar we want to register only SecondBar? Sorting:

container.Register(
    AllTypes.FromThisAssembly().Pick()
        .WithService.DefaultInterface()
        .For<IBar>().Select<SecondBar>())

: , . (, , ). , ( ). . ?

+5
2

:

container.Register(
    AllTypes.FromThisAssembly().Pick()
        .WithService.DefaultInterface())
        .ConfigureFor<IBar>(c => 
            c.If((k, m) => m.Implementation == typeof(SecondBar)));

SecondBar impl IBar. , , , , .

:

public static BasedOnDescriptor Select<TService, TImpl>(this BasedOnDescriptor desc)
{
    return desc.ConfigureFor<TService>(c => c.If((k, m) => m.Implementation == typeof(TImpl)));
}

public static BasedOnDescriptor Ignore<TService>(this BasedOnDescriptor desc)
{
    return desc.ConfigureFor<TService>(c => c.If((k, m) => false));
}

:

container.Register(
    AllTypes.FromThisAssembly().Pick()
        .WithService.DefaultInterface())
        .Select<IBar, SecondBar>()
        .Ignore<ISomeService>()

. , . @Krzysztof Koźmic: ?:)

+3

. , , .

container.Register(
    AllTypes.FromThisAssembly()
        .Where(t => t.Namespace != "Acme.Tests")
        .WithService.DefaultInterface())
+5

All Articles