开发者

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 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 a开发者_如何学Pythonnd 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.


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...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜