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

回眸只為那壹抹淺笑 提交于 2019-12-10 13:40:07

问题


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?


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/875670/castle-windsor-fluent-registration-what-does-pick-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!