Search code examples
clean-architecturemediatr

Can Query send another Query in MediatR?


I read that command shouldn't send another command, and for that I should use INotification. But how is it with Queries? Can one Query send another Query? Or maybe should I refactor another Query into service? Are any Clean architecture restrictions to this?


Solution

  • You can do it either in your endpoint which is a controller or in the corresponding handler. If you do it in the controller you can simply create your second request based on the response you got from the first MediatR sending and call the second MediatR send method. Now you can perform an in-memory join of the results and send them back to the client. If you always need to perform a query immediately after the first query it is recommended to perform it in the corresponding handler and aggregate the result and send them as one response.