UnityContainer, 어린이 컨테이너 주입
-
21-08-2019 - |
문제
나는 Unity Application Block에 익숙하지 않으며 다음을 구현하려고 노력하고 있습니다. 부모와 자식 컨테이너가 있습니다. 하위 컨테이너의 새 인스턴스를 해결하면 CreateChildContainer 메소드를 사용하여 새 UnityContainer를 생성자에 주입하고 싶습니다.
public class RootContainer
{
private IUnityContainer _container;
public RootContainer()
{
_container = new UnityContainer();
_container.RegisterType<IChildContainer, ChildContainer>();
}
}
public interface IChildContainer { }
public class ChildContainer : IChildContainer
{
private IUnityContainer _container;
/* I want to inject the parent.CreateChildContainer() into this constructor */
public ChildContainer(IUnityContainer container)
{
_container = container;
}
}
해결책
내가 보는 가장 간단한 해결책은 ChildContainer의 생성자에게 상위 용기를 부여하고 CTOR 내에서 CreateChildContainer () 메소드를 호출하는 것입니다.
public class ChildContainer2 : IChildContainer
{
private IUnityContainer _container;
public ChildContainer(IUnityContainer parent)
{
_container = parent.CreateChildContainer();
}
public IUnityContainer Container { get { return _container; } }
}
그러나 왜 어린이 컨테이너를 서비스로 만들고 싶습니까? 앱의 각 구성 요소가 필요한 경우 자체 컨테이너를 자체로 만들면 더 나을 수 없습니까?
제휴하지 않습니다 StackOverflow