Combining CQRS and Event Sourcing
CQRS and Event Sourcing are complementary patterns that work well together in building robust, scalable, and maintainable distributed systems.
The command handler in the CQRS architecture is responsible for validating write requests. If a command is valid, an event is persisted to an event store, which is the core of the Event Sourcing pattern. An example of how the CQRS command and Event Sourcing integrate is shown in Figure 9.5:
Figure 9.5 – CQRS command and Event Sourcing
The service receives a command from the requester. The service requires the current state of the aggregate, which is rebuilt by replaying events retrieved from the Event Store. The command passes the validation, so a new event is generated. The new event is played on the aggregate to generate a new state. The new event is appended to the Event Store and the updated aggregate is returned to the requester.
Event Sourcing answered the question...