Following scenario:
We use the Fluent API to register all components in an assembly and two components tyepof(A) with named keys. Another class B with two properties typeof(A) should get the named components injected.
Sample:
public class A : IA {}
public class B : IB
{
[Named("first")]
public IA First { get; set; }
[Named("second")]
public IA Second { get; set; }
}
// ...
container.Register(Component.For<IA>().Instance(new A(value1)).Named("first"));
container.Register(Component.For<IA>().Instance(new A(value2)).Named("second"));
// ...
var b = container.Resolve<IB>(); // named instances of A get injected to B using the Named attribute
Is this possible with an attribute like Named or only with the Xml Config?
The standard way to do this in Windsor is using service overrides. In your example, when you register
Byou’d do it like this:(there are other ways to express this, check the linked docs)
Using a
Namedattribute as you propose pollutes the code with unrelated concerns (Bshould not care about whatAs get injected)