开发者

Castle windsor registration

开发者 https://www.devze.com 2022-12-30 00:58 出处:网络
interface IUserService class LocalUserService : IUserService class RemoteUserService : IUserService interface IUserRepository
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

精彩评论

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