views:

706

answers:

2

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?

+7  A: 

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.

Mauricio Scheffer
Mausch - have you thought about contributing to Castle docs about fluent interfce? It's not the first question like this you answer.
Krzysztof Koźmic
I'll give it a try
Mauricio Scheffer
+2  A: 

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.

bbmud
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(..)
George Mauer