의존성 주입에 꽤 익숙하지 않은데 이것이 반 패턴인지 알아 내려고합니다.종속성 주입을 사용하여 종속성 인젝터 주입
의 내가 3 어셈블리 있다고 가정 해 봅시다 :
Foo.Shared - this has all the interfaces
Foo.Users - references Foo.Shared
Foo.Payment - references Foo.Shared
Foo.Users는 Foo.Payment 내에 구축 된 객체를 필요로하고 Foo.Payment도 Foo.Users에서 물건을 필요로한다. 이것은 일종의 순환 종속성을 만듭니다.
내가 사용하고있는 Dependency Injection 프레임 워크를 프록시하는 Foo.Shared에 인터페이스를 정의했습니다 (이 경우 NInject).
public class DependencyResolver:IDependencyResolver
{
private readonly IKernel _kernel;
public DependencyResolver(IKernel kernel)
{
_kernel = kernel;
}
public T Get<T>()
{
return _kernel.Get<T>();
}
}
구성은 다음과 같습니다 :
public class MyModule:StandardModule
{
public override void Load()
{
Bind<IDependencyResolver>().To<DependencyResolver>().WithArgument("kernel", Kernel);
Bind<Foo.Shared.ISomeType>().To<Foo.Payment.SomeType>(); // <- binding to different assembly
...
}
}
이 날 Foo.Payment.SomeType
의 새로운 객체를 인스턴스화 할 수 있습니다 컨테이너 응용 프로그램에서
public interface IDependencyResolver
{
T Get<T>();
}
는,이 인터페이스의 구현이 Foo에서 직접 참조 할 필요가없는 사용자 :
public class UserAccounts:IUserAccounts
{
private ISomeType _someType;
public UserAccounts(IDependencyResolver dependencyResolver)
{
_someType = dependencyResolver.Get<ISomeType>(); // <- this essentially creates a new instance of Foo.Payment.SomeType
}
}
이렇게하면 UserAccounts
클래스의 정확한 종속성이이 인스턴스에 어떤 영향을 미치는지 명확하지 않으므로 이것이 좋지 않다고 생각됩니다.
어떻게 그럴 수 있습니까?
의견이 있으십니까?
+1 혀 트위스터 제목 전용입니다. – womp
여기에 같은 제목을 붙입니다 : –