0

(.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)
   { 
       ...
   }
}
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
jeancallisti
  • 1,046
  • 1
  • 11
  • 21
  • 1
    The interface `IRequestHandler` requires that the Handle method returns `Handyman.Mediator.Void`. If you want to implement that interface, you have to comply with this. You can create a base class with an abstract function `public Task Handle(...)` and implement `IRequestHandler` there, if you really want to deal with this. – patrick May 30 '23 at 09:36
  • A quick look at the [source](https://github.com/JonasSamuelsson/Handyman/blob/master/src/Handyman.Mediator/src/IRequestHandler.cs) shows up that it is not a tweak but how to do it – Sir Rufo May 30 '23 at 09:40
  • @patrick look at my edit. Just not sure why it works with the class but not the interface. there's some syntactic intricacy there. – jeancallisti May 30 '23 at 09:45
  • Have again a look at the [source](https://github.com/JonasSamuelsson/Handyman/blob/master/src/Handyman.Mediator/src/RequestHandler.cs) it is exactly what patric describes. The class implements the interface and routes the method with `Task` to the astract returning `Task` – Sir Rufo May 30 '23 at 09:48
  • Thanks. Indeed, it makes perfect sense. My code is much much more complicated than the example, and I guess it becomes obvious after shedding every misleading code off. Thanks again! – jeancallisti May 30 '23 at 10:48

0 Answers0