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:
- How automatically register those handlers by autofac?
- I have not happy with service constructor. any better idea?