question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AsyncRequestHandler hides the Handle method

See original GitHub issue

The AsyncRequestHandler class hides the Handle method, requiring tests to explicitly type the tested handler as IRequestHandler<TRequest>.

Consider following code:

public class MyRequest : IRequest {
}

public class MyHandler: AsyncRequestHandler<MyRequest> {
  protected override Task Handle(MyRequest request, CancellationToken cancellationToken) {
    // Do some work
  }
}

To test the code, one would expect following:

[Fact]
public async Task MyTest {
  var handler = new MyHandler();
  await handler.Handle(new MyRequest(), default);
  // Verify results
}

This results in compilation error CS0122: ‘MyHandler.Handle(MyRequest, CancellationToken)’ is inaccessible due to its protection level’

The workaround is to explicitly type the handler, making the code unnecessary bloated, especially with longer request and handler class names:

[Fact]
public async Task MyTest {
  IRequestHandler<MyRequest> handler = new MyHandler();
  await handler.Handle(new MyRequest(), default);
  // Verify results
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jbogardcommented, Feb 3, 2022

Honestly, I’d rather just remove these classes than anything else. I don’t use them anyway and they just seem to cause more problems than solve.

3reactions
cheesicommented, Jan 20, 2022

Hello!

For me personally, the confusing thing about this is not the AsyncRequestHandler, it abstracts away the Unit.Value, which is a good thing imho, as for me the Unit.Value is the actual problem. I can understand, that it was most likely added to simplify things, but if you have never worked with MediatR before, you will find this thing very confusing.

Why is it named Unit.Value? Especially if it hasn’t any value? Wouldn’t NoValue be a better name? Also I would not remove AsyncRequestHandler. Adding a return Unit.Value to each command doesn’t provide any business value, it is an necessary implementation detail, that should be hidden way.

From my point of view there would be two good ways to go forward:

  • Keep AsyncRequestHandler and make the Handle public, to make the unit testing easier without the workaround of setting the variable type to IRequestHandler<MyRequest>. @jbogard Do you have any problems with the execution time of your unit tests? We did a quick test setup on our side and saw the setup alone took about 160ms, which is too slow in our opinion (our “limit” is 100ms, which we try do achieve when it is possible)
  • Another way I could see, would be using Default interface methods. Then the AsyncRequestHandler could be deprecated and the abstraction of it could be moved into the interface. I guess this would avoid any confusion between the interface and the abstract class.

What are your thoughts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Cannot access method 'Handle' MediatR due to its ...
1 Answer 1 ... Declare the handler variable as of type IRequestHandler<AddUserDeviceCommand> . AsyncRequestHandler<TRequest> implements ...
Read more >
A brand new website interface for an even better experience!
AsyncRequestHandler hides the Handle method.
Read more >
Do NOT stop worrying about blocking in async functions!
One possible way to reimplement this with better utilization of the CPU cores is to perform these blocking operations in a dedicated thread...
Read more >
puppeteer-core
9, * The Accessibility class provides methods for inspecting Chromium's ... 3026, * Adds an async request handler to the processing queue.
Read more >
two kinds of messages dispatched by MediatR
Request/response messages, dispatched to a single handler ... 也就是没有返回值, use the AsyncRequestHandler<TRequest> base class:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found