두 컨테이너로 작업 할 때 Unity 컨테이너에서 이상한 동작이 발생합니다. 둘 다 동일한 장식 자에 등록됩니다. 코드 예제가 더 명확해질 것입니다. Unity : 두 인터페이스에 동일한 유형 등록
나는 다음과 같은 클래스 계층이 있습니다public interface IBaseInterface
{
}
public interface IInterface1: IBaseInterface
{
}
public interface IInterface2: IBaseInterface
{
}
public class Interface1Impl : IInterface1
{
}
public class Interface2Impl : IInterface2
{
}
public class BaseInterfaceDecorator: IInterface1,IInterface2
{
private readonly IBaseInterface baseInterface;
public BaseInterfaceDecorator(IBaseInterface baseInterface)
{
this.baseInterface = baseInterface;
}
}
public class MyClass
{
private readonly IInterface1 interface1;
public MyClass(IInterface1 interface1)
{
this.interface1 = interface1;
}
}
을 그리고 이것은 등록 코드 :
var container = new UnityContainer();
container.RegisterType<IInterface1, BaseInterfaceDecorator>(
new InjectionConstructor(
new ResolvedParameter<Interface1Impl>()));
container.RegisterType<IInterface2, BaseInterfaceDecorator>(
new InjectionConstructor(
new ResolvedParameter<Interface2Impl>()));
var dependency = container.Resolve<MyClass>();
내가 Interface2Impl 대신 Interface1Impl와 BaseInterfaceDecorator납니다 MyClass의 해결. 나에게 이상하게 보입니다. 설명 할 수 있니?
; 내부적으로 Unity는 'to'유형의 빌드 키를 캐시합니다 (예 : 일반적인 서명에서 'TTo'). 다음에 'TFrom'유형을 확인하면 빌드를 수행하는 데 'TTo'의 빌드 키가 사용됩니다. 이 빌드 키는 평생 관리자와도 연관되어 있으므로 Unity는 TFTo의 ID와 상관없이 TTo에 대한 모든 하위 집합에 대해 처음 사용 된 수명 관리자 (Build Up)를 사용하는 것으로 나타납니다. 이것은 Unity의 빌드 키. 해결 방법은 이름이 같은 등록을 사용하는 것인데, 이는 공변 (covariant) 유형 신원 확인의 요점을 무효화합니다. –