Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 319
George Mauer Avatar asked May 17 '09 22:05

George Mauer


2 Answers

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.

like image 172
Mauricio Scheffer Avatar answered Oct 23 '22 23:10

Mauricio Scheffer


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.

like image 23
bbmud Avatar answered Oct 23 '22 23:10

bbmud