5

I'm just wondering if the design I will be trying to implement is valid CQRS.

I'm going to have a query handler that itself will send more queries to other sub-handlers. Its main task is going to aggregate results from multiple services. Is this ok to send queries from within handlers? I can already think of 3 level deep hierachies of these in my application.

mbudnik
  • 2,087
  • 15
  • 34

2 Answers2

2

No, MediatR is designed for a single level of requests and handlers. A better design may be to create a service/manager of some kind which invokes multiple, isolated queries using MediatR and aggregate the results. The implementation may be similar to what you have in mind, except that it's not a request handler itself but rather an aggregation of multiple request handlers.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
0

This will badly affect the system's resilience and compute time and it will increase coupling.

If any of the sub-handlers fails then the entire handler will fail. If the queries are send in a synchronous way then the total compute time is sum of the individual queries times.

One way to reuse the sub-handlers is to query them in the background, outside the client's request, it possible. In this way, when a client request comes you already have the data locally, increasing the resilience and compute time. You will be left only with the coupling but it could worth it if the reuse is heavier than the coupling.

I don't know if any of this is possible in MediatR, there are just general principles of system architecture.

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54