Castle Windsor Fluent Registration - What does Pick() do?
Asked Answered
F

2

6

When using auto-registration with castle windsor I see people doing things like

_container.Register(
  AllTypes.Pick().FromAssembly(Assembly.GetExecutingAssembly())
    .WithService.FirstInterface());

For the life of me I can't figure out what the Pick() method does nor can I find any documentation. Can anyone explain it to me?

Fondness answered 17/5, 2009 at 22:40 Comment(0)
H
9

Pick(IEnumerable<Type>) is a synonym for From(IEnumerable<Type>), i.e. it selects the specified types as registration targets.

AllTypes.Pick() is the same as AllTypes.Of<object>(), so it effectively selects all types.

AllTypes.Pick().FromAssembly(Assembly.GetExecutingAssembly()) will select ALL types in the executing assembly (you can then filter, of course)

As usual, take a look at the fluent API wiki and/or test case for more information.

Hernandes answered 17/5, 2009 at 23:40 Comment(1)
Mausch - have you thought about contributing to Castle docs about fluent interfce? It's not the first question like this you answer.Bookcraft
I
2

It is sort of the starting point in this fluent API for chosing which types will be automatically registered in the container.

Container.Register(
        AllTypes.Pick()
        .FromAssemblyNamed("MyAssembly")
        .If(t => t.Name.EndsWith("ABC"))
        .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
        .WithService.Select(i => typeof(I))
    );

In this example all types picked from MyAssembly with name ending with "ABC" will be added to the container with Transient lifestyle as services of type I. The example comes from this question.

This is a declarative approach in the form of internal DSL. With this kind of API, methods are used to sort of configure the behavior that will be executed later. To achieve this, the methods return builders guiding through the steps of configuration, while the actual work is done at the end.

Invocate answered 17/5, 2009 at 23:44 Comment(1)
The problem is that in the above example it would seem that instead of using AllTypes.Pick().FromAssemblyNamed(...).If(..) you can do the shorter and more standardized AllTypes.FromAssemblyNamed(...).Where(..)Fondness

© 2022 - 2024 — McMap. All rights reserved.