AsyncRequestHandler hides the Handle method
See original GitHub issueThe 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:
- Created 3 years ago
- Reactions:11
- Comments:13 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Hello!
For me personally, the confusing thing about this is not the
AsyncRequestHandler
, it abstracts away theUnit.Value
, which is a good thing imho, as for me theUnit.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’tNoValue
be a better name? Also I would not removeAsyncRequestHandler
. Adding areturn 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:
AsyncRequestHandler
and make theHandle
public, to make the unit testing easier without the workaround of setting the variable type toIRequestHandler<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)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?