필자는 필자가 필요로하는보다 복잡한 몇 가지 예제를 읽었으며 간단하고 간결한 패턴으로이 문제를 해결하는 데 어려움을 겪고 있습니다.AutoFac으로 매개 변수를 받아들이는 공장 주입하기
인터페이스 이름이 ICustomService이고 ICustomService가 여러 개 구현되어 있다고 가정 해 보겠습니다. 또한 런타임에 ICustomService가 매개 변수를 기반으로 사용할 것인지를 결정해야하는 클래스 Consumer가 있습니다. 다음과 같이
그래서 나는 클래스를 생성 :
public class Consumer
{
private CustomServiceFactory customServiceFactory;
public Consumer(CustomServiceFactory _customServiceFactory)
{
customServiceFactory = _customServiceFactory;
}
public void Execute(string parameter)
{
ICustomService Service = customServiceFactory.GetService(parameter);
Service.DoSomething();
}
}
public class CustomServiceFactory
{
private IComponentContext context;
public CustomServiceFactory(IComponentContext _context)
{
context = _context;
}
public ICustomService GetService(string p)
{
return context.Resolve<ICustomService>(p); // not correct
}
}
public class ServiceA : ICustomService
{
public void DoSomething()
{
}
}
public class ServiceB : ICustomService
{
public void DoSomething()
{
}
}
장점 내 공장 인터페이스를 구현하는 데 있나요? Consumer.Execute ("A")가 WorkerA 및 Consumer.Execute ("B")에서 DoSomething을 호출하도록 Workf에서 DoSomething을 호출하도록 공장을 수정하고 Autofac에 이러한 클래스를 등록하려면 어떻게해야합니까?
는
고맙습니다! 나는 Func를 직접 등록하는 방법을 제외하고는 모든 것을 이해합니다. 그리고 나는 그것을 해결할 수 있다고 확신합니다. –