2
interface IUserService 
class LocalUserService : IUserService 
class RemoteUserService : IUserService 

interface IUserRepository 
class UserRepository : IUserRepository 

IUserService 클래스가 IUserRepository에 종속 된 다음 인터페이스와 클래스가있는 경우. 호출하여 내가 원하는 서비스를성 윈저 등록

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을 해결하도록 컨테이너에 요청하는 것입니다.

답변

4

삽입 할 구성 요소를 결정하려면 (밀어 넣기) IHandlerSelector을 사용하십시오.

어떤 구성 요소를 가져올 것인지 결정하려면 TypedFactoryFacility를 사용하십시오.

및 사이드 네로서.

AddComponent과 등록 사이에 차이가 container.Register(Component.For...)

+0

사용 AddComponent 사용하지 마십시오? – nivlam

+0

가장 큰 차이점은 Register는 훨씬 더 유연하며,'AddComponent'는 다음 버전부터 쓸모 없게되고 나중에 제거된다는 것입니다. –