(.Net 6, C# 10, Handyman.Mediator 11.3)
Some context :
class A {
...
}
class RequestWithResponse : IRequest<A> {
int SomeParam { get; set; }
}
interface IHandlerWithResponse : IRequestHandler<Request, A> { }
class RequestWithoutResponse : IRequest {
int SomeParam { get; set; }
}
interface IHandlerWithoutResponse : IRequestHandler<Request> { }
Note: It's Handyman.Mediator 11.3
who provides those :
IRequest // This is really IRequest<Void>
IRequest<Tresponse>
IRequestHandler<TRequest> // This is really IRequestHandler<TRequest, Void>
IRequestHandler<TRequest, TResponse>
Now I'm trying to implement both as async methods :
public class HandlerWithResponse : IHandlerWithResponse {
public async Task<A> Handle(RequestWithResponse request, CancellationToken cancellationTokeb) {
...
}
}
public class HandlerWithoutResponse : IHandlerWithoutResponse {
public async Task Handle(RequestWithoutResponse request, CancellationToken cancellationTokeb) {
...
}
}
HandlerWithResponse works as expected.
Problem : HandlerWithoutResponse causes the following C# error
'HandlerWithoutResponse ' does not implement interface member 'IHandlerWithoutResponse <Request, Void>.Handle(Request, CancellationToken)'.
'HandlerWithoutResponse .Handle(Request, CancellationToken)' cannot implement 'IHandlerWithoutResponse <Request, Void>.Handle(Request, CancellationToken)' because it does not have the matching return type of 'Task'.
I can do it with the following tweaking (add Handyman.Mediator.Void in two places) but it's really ugly (and the whole purpose of my abstraction was to leave the least possible implementation details to the imagination) :
public class HandlerWithoutResponse : IHandlerWithoutResponse {
public async Task<Handyman.Mediator.Void> Handle(RequestWithoutResponse request, CancellationToken cancellationTokeb) {
...
return new Handyman.Mediator.Void();
}
}
Is there a minimalistic way of doing this?
This thread is tip-toeing around it but does not provide this specific answer.
It mentions AsyncRequestHandler
but there's no interface IAsyncRequestHandler
EDIT: For context
My original endeavour was to introduce interfaces. The reason why this whole thing puzzles me is that it works when I use the out-of-the-box RequestHandler
class directly instead of an interface:
public class HandlerWithoutResponse:
RequestHandler // <-- not an interface
<RequestWithoutResponse>
{
protected override async Task Handle(Request request, CancellationToken cancellationToken)
{
...
}
}