0

I try to resolve a handler but because of unknown reasons I can not get it work.
Here is my code: I have an interface as IRequestHandler as below

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

I have an abstract class as TransactionRequestHandler to implement 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);
}

There is an RequestTypeHandler which need that abstract class TransactionRequestHandler

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

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

I have a service which I try to resolve access to RequestTypeHandler. I did not manage to resolve it by AutoFac! Here is my service as 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 [...];
    }
}

My question are:

  1. How automatically register those handlers by autofac?
  2. I have not happy with service constructor. any better idea?
Steven
  • 166,672
  • 24
  • 332
  • 435
ShrnPrmshr
  • 353
  • 2
  • 5
  • 17
  • What does your Autofac config look like then? – DavidG Aug 02 '17 at 10:36
  • I have tried many things .. this is my last ones: builder.RegisterGeneric(typeof(IRequestHandler<,>)).Named("handler", typeof(IRequestHandler<,>)); builder.RegisterGenericDecorator( typeof(TransactionRequestHandler<,>), typeof(IRequestHandler<,>), fromKey: "handler"); – ShrnPrmshr Aug 02 '17 at 10:42
  • Please include in the question what you have tried, what Autofac documentation did you read related to your problem, what your configuration looks like, what the exception is you are getting, possibly including stack trace. – Steven Aug 02 '17 at 10:44
  • this is exactly my concern. I am not getting any error. I do not know how to write it. that last ones ok I added. but inside my service how should I refer to that specific IRequestHandler<,>. then I need to write every single request type of IRequestHandler<,> as parameter in service's constructor which seems too ugly!! – ShrnPrmshr Aug 02 '17 at 10:48
  • @Steven thanks for reference .. I have one extra abstract class in this example not sure if it is duplicated.. but let me try maybe I can fix it this time. – ShrnPrmshr Aug 02 '17 at 11:20
  • Your base class doesn't change the question (apart from that you shouldn't use base classes IMO). The following registration should work: `builder.RegisterAssemblyTypes(assemblies).AsClosedTypesOf(typeof(IRequestHandler<,>));` – Steven Aug 02 '17 at 11:23
  • Abstract class keep the repeated code for those handlers. just a small part of it is difference which I get from each handler. About Autofac, yes exactly I am trying that – ShrnPrmshr Aug 02 '17 at 11:26

0 Answers0