问题
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