Castle Windsor Registration

interface IUserService
class LocalUserService : IUserService
class RemoteUserService : IUserService

interface IUserRepository
class UserRepository : IUserRepository

      

If I have the following interfaces and classes where the classes IUserService

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 you register these components so that the component IUserService

can "choose" which repository to deploy at runtime? My idea is to let the user choose which repository to request from by providing 3 radio buttons (or whatever) and prompting the container to allow a new one every time IUserService

.

+2


a source to share


1 answer


If you want to decide which component to push, you use IHandlerSelector

s.

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



and as a side element.

Do not use AddComponent

, usecontainer.Register(Component.For...)

+4


a source







All Articles