Castle windsor registration

Posted by nivlam on Stack Overflow See other posts from Stack Overflow or by nivlam
Published on 2010-05-13T20:59:45Z Indexed on 2010/05/13 21:04 UTC
Read the original article Hit count: 230

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.

© Stack Overflow or respective owner

Related posts about inversion-of-control

Related posts about dependency-injection