How to resolve a multiple service implementation

22 views
Skip to first unread message

Emanuele DelBono

unread,
May 14, 2010, 11:18:10 AM5/14/10
to castle-pro...@googlegroups.com
Hi again
I have a question about Windsor.

I have ClassA that depends on IService and I have 2 implementations of
IService (Service1 and Service2):

public class ClassA : IClassA
{
public ClassA(IService service)
{...}
}
public Service1 : IService
public Service2 : IService

The implementations is provided by a factory method.
I registered all the components: ClassA, Service1 and Service2.

I would like to resolve ClassA with Service1 or Service2 depending on
the context of the application, sometimes ClassA needs Service1 and
other times needs Service2.

Do you have any hints?
Thanks!
ema
http://blog.codiceplastico.com

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

Krzysztof Koźmic

unread,
May 14, 2010, 11:41:18 AM5/14/10
to castle-pro...@googlegroups.com
use IHandlerSelector or TypedFactoryFacility

Emanuele DelBono

unread,
May 17, 2010, 9:48:42 AM5/17/10
to castle-pro...@googlegroups.com
Hi Krzysztof
I already try with IHandlerSelector and it works fine for the first
level resolution.
I have this configuration:

Kernel.Register(Component.For<IService>()
.Named("service1")
.UsingFactoryMethod(() => ServiceFactory.CreateService1());

Kernel.Register(Component.For<IService>()
.Named("service2")
.UsingFactoryMethod(() => ServiceFactory.CreateService2());

Kernel
.Register(Component.For<ClassA>().ImplementedBy<ClassA>());

Where ClassA depends on IService.

I would like to resolve ClassA sometimes with Service1 and sometimes
with Service2, something like:

kernel.Resolve<ClassA>("with Service1"); // don't know the sintax!
kernel.Resolve<ClassA>("with Service2");


thx!!

ema
http://blog.codiceplastico.com



2010/5/14 Krzysztof Koźmic <krzyszto...@gmail.com>:

Bailey Ling

unread,
May 17, 2010, 11:17:01 AM5/17/10
to castle-pro...@googlegroups.com
you can try something like this:

k.Resolve<ClassA>(new { service = k.Resolve<IService>("service1") });

DynamicParameters is an optional as well
Reply all
Reply to author
Forward
0 new messages