2
interface IUserService
class LocalUserService : IUserService
class RemoteUserService : IUserService
interface IUserRepository
class UserRepository : IUserRepository
如果我有以下接口和类,其中IUserService
类对IUserRepository
有依赖关系。通过调用Castle windsor注册
container.AddComponent("LocalUserService", typeof(IUserService), typeof(LocalUserService));
container.AddComponent("RemoteUserService", typeof(IUserService), typeof(RemoteUserService));
container.AddComponent("UserRepository", typeof(IUserRepository), typeof(UserRepository));
...并得到我想要的服务:我可以做这样的事情注册这些组件
IUserService userService = container.Resolve<IUserService>("RemoteUserService");
但是,如果我有以下的接口和类:
interface IUserService
class UserService : IUserService
interface IUserRepository
class WebUserRepository : IUserRepository
class LocalUserRepository : IUserRepository
class DBUserRepository : IUserRepository
如何注册这些组件,以便IUserService
组件可以“选择”在运行时注入哪个存储库?我的想法是允许用户通过提供3个单选按钮(或任何)来选择要查询的存储库,并要求容器每次解析新的IUserService
。
有AddComponent和寄存器之间的区别吗? – nivlam 2010-05-13 21:16:20
主要区别在于Register更灵活,并且'AddComponent'将从下一个版本开始过时,并在稍后移除。 – 2010-05-14 06:07:07