views:

66

answers:

1
interface IUserService
class LocalUserService : IUserService
class RemoteUserService : IUserService

interface IUserRepository
class UserRepository : IUserRepository

If I have the following interfaces and classes, where the IUserService classes have a dependency on IUserRepository. I can register these components by doing something like:

container.AddComponent("LocalUserService", typeof(IUserService), typeof(LocalUserService));
container.AddComponent("RemoteUserService", typeof(IUserService), typeof(RemoteUserService));
container.AddComponent("UserRepository", typeof(IUserRepository), typeof(UserRepository));

... and get the service I want by calling:

IUserService userService = container.Resolve<IUserService>("RemoteUserService");

However, if I have the following interfaces and classes:

interface IUserService
class UserService : IUserService

interface IUserRepository
class WebUserRepository : IUserRepository
class LocalUserRepository : IUserRepository
class DBUserRepository : IUserRepository

How do I register these components so that the IUserService component can "choose" which repository to inject at runtime? My idea is to allow the user to choose which repository to query from by providing 3 radio buttons (or whatever) and ask the container to resolve a new IUserService each time.

+3  A: 

If you want to decide which component to inject (push) you use IHandlerSelectors.

If you want to decide which component to pull you use TypedFactoryFacility.

and as a sidenote.

Don't use AddComponent, use container.Register(Component.For...)

Krzysztof Koźmic
Is there a difference between AddComponent and Register?
nivlam
Major difference is that Register is far more flexible, and that `AddComponent` will be obsolete starting with next version, and removed later on.
Krzysztof Koźmic