컨트롤러를 만들기 위해 .NET 웹 API 2 사이트에서 DryIoc을 사용하려고합니다. 컨트롤러에 프로세서가 필요하고 프로세서에 스토리지 클래스의 인스턴스가 두 개 필요한 상황이 있습니다. 다음은 그 기본이다 :동일한 인터페이스의 다른 인스턴스가 필요한 경우 DryIoc 컨테이너를 어떻게 설정합니까?
public interface IStorage
{
IEnumerable<string> List();
void Add(string file);
void Remove(string file);
}
public class FileSystemStorage : IStorage
{
// Implement to store on file system.
}
public class S3Storage : IStorage
{
// Implement to store in S3 bucket.
}
public interface IProcessor
{
void Process();
}
public class Processor(IStorage sourceStorage, IStorage targetStorage)
{ // Implement a process that interacts with both storages }
public class ProcessController : ApiController
{
private readonly IProcessor processor;
public ProcessController(IProcessor processor)
{
this.processor = processor;
}
}
그래서, 내 IOC 컨테이너 (DryIoc)는 인터페이스 IStorage
에 대한 두 개의 서로 다른 클래스를 사용이 필요합니다.
var sourceStorage = new FileSystemStorage();
var targetStorage = new S3Storage();
var processor = new Processor(sourceStorage, targetStorage);
// And then have DryIoc dependency resolver create
// controller with this processor.
그러나, 등록하는 일반적인 방법은 그냥 작동하지 않습니다 : 나는 주사를 종속성 새로 온
var c = new Container().WithWebApi(config);
// Need two different implementations...
c.Register<IStorage, ???>();
// And even if I had two instances, how would
// the processor know which one to use for what parameter?
c.Register<IProcessor, Processor>();
을 그래서, 내가 원하는 것은이 같은 뭔가를 설치 IOC이다 컨테이너 및 대부분의 문서는 매우 추상적입니다. 나는 그들을 grokking 아니에요. 어떻게 이뤄지나요? 이 설정을 깰 것 매개 변수 이름을 변경 깨지기 쉬운 접근 원인이라고
c.Register<IStorage, Foo>(serviceKey: "in");
c.Register<IStorage, Bar>(serviceKey: "out");
c.Register<IProcessor, Processor>(made: Parameters.Of
.Name("source", serviceKey: "in")
.Name("target", serviceKey: "out"));
문제 :
고마워요! 당신은 나를 위해 DryIoc과의 격차를 좁 혔고 디자인을 재검토하기 위해 나를 밀어 붙였습니다 ... 점점 더 두 인터페이스로 분리해야하는 것처럼 보입니다. –