Here is how my assembly (TestAssembly) looks
Class BaseClass
{
....
....
}
Interface I1
{
....
....
}
Interface I2 : I1
{
....
....
}
Interface I3 : I1
{
....
....
}
Class A : BaseClass, I2
{
....
....
}
Class B : BaseClass, I3
{
....
....
}
I am using Castle Windsor to Register and Resolve all these components.
container.Register(AllTypes.FromAssemblyNamed("TestAssembly")
.BasedOn<I1>()
.LifestyleSingleton());
Down the line I am trying to ResolveAll Types of I1 (Interface I1) using
container.ResolveAll<I1>();
in hopes of getting an array of objects A and B. Instead I get an empty array. Whats wrong?
I believe you need to tell the container which services to associate with the interface, something like:
container.Register(AllTypes.FromAssemblyNamed("TestAssembly")
.BasedOn<I1>()
.WithServiceBase()
.LifestyleSingleton());
I seem to have found answers for my questions (see my comments to Hans Jonus's response).
In case of interface inheritance, there is fluent API where in you can specify what interface the service should be using.
Using the example mentioned in the question:
container.Register(Classes.FromAssemblyNamed("TestAssembly")
.BasedOn<I1>()
.WithService.FirstInterface());
This lets you resolve a specific instances of classes A and B using
I2 ObjA = container.Resolve<I2>();
I3 ObjB = container.Resolve<I3>();
Or, if you want all the instances which are implementing the base interface (I1), you have to register in this way.
container.Register(Classes.FromAssemblyNamed("TestAssembly")
.BasedOn<I1>()
.WithService.AllInterfaces()
);
This way you can do all you want, I mean.
I2 ObjA = container.Resolve<I2>();
I3 ObjB = container.Resolve<I3>();
I1[] ObjArray = container.ResolveAll<I1>();
But this increases the size of the container. I am not sure how it impacts the performance. I will post my findings as I figure out. I appreciate any comments on this. Thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With