Search code examples
architecturecqrsclean-architecture

Query database in Command handler within CQRS pattern


I am developing an application based on CleanArchitecture template which uses CQRS. What I have read about CQRS is that read and write are separated. Does it means that I should avoid query database in Command Handlers? For example in UpdateCommandHandler, I need to first get the record then update it.


Solution

  • You should check out event-sourcing if you're going to get into CQRS - it simplifies this considerably. With an event-sourcing approach, you would just write the event containing the new data and wouldn't have to update anything. Therefore there would be no read.

    If the query-side needs to know the current state of your data then it would aggregate all the events relating to that data and 'sum' them to find the current state, which it would report. This is a similar approach to bookkeeping, which people have practised for millennia.