Autofac - 解析IRequestHandler <specificrequest,specificresponse =“”>

时间:2017-08-02 10:23:21

标签: c# autofac

我尝试解决处理程序,但由于原因不明,我无法解决问题 这是我的代码: 我有一个接口作为IRequestHandler,如下所示

public interface IRequestHandler<in TRequest, out TResponse> 
{
    TResponse Handler(TRequest request);
}

我有一个抽象类作为TransactionRequestHandler来实现IRequestHandler

public abstract class TransactionRequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
    where TRequest: IRequest
    where TResponse: IResponse
{
    public TransactionRequestHandler() { //does something}

    public TResponse Handler(TRequest request)
    {
       //Does something with MethodOne() and MethodTwo()
    }

  protected abstract SomeType MethodOne(SomeType request);
  protected abstract SomeType MethodTwo(SomeType st);
}

有一个RequestTypeHandler需要抽象类TransactionRequestHandler

 public class RequestTypeHandler : TransactionRequestHandler<RequestType, ResponseType>
{
    protected override SomeType MethodOne(SomeType request)
    {
        return [SomeType];
    }

    protected override SomeType MethodTwo(SomeType st)
    {
        return [SomeType];
    }
}

我有一个服务,我尝试解析对RequestTypeHandler的访问。我无法通过AutoFac解决它! 这是我作为TransactionService的服务

 public class TransactionService : ITransactionService
{
    private readonly IRequestHandler<TypeOneRequest, TypeOneResponse> _handlerOne;
    private readonly IRequestHandler<TypeTwoRequest, TypeTwoResponse> _handlerTwo;

    public TransactionService(
        IRequestHandler<TypeOneRequest, TypeOneResponse> handlerOne,
        IRequestHandler<TypeTwoRequest, TypeTwoResponse> handlerTwo)
    {
        _handlerOne= handlerOne;
        _handlerTwo= handlerTwo
    }

    public TypeOneResponse MethodOne(TypeOneRequest request)
    {
        var handler= _handlerOne.Handle();
        return [...];
    }

    public TypeTwoResponse MethodTwo(TypeTwoRequest request)
    {
        var handler= _handlerTwo.Handle();
        return [...];
    }
}

我的问题是:

  1. 如何通过autofac自动注册这些处理程序?
  2. 我对服务构造函数不满意。还有什么好主意吗?

0 个答案:

没有答案