Member-only story
DDD — Command Query Responsibility Segregation (CQRS)
Commands are complex, Queries are simple
Previously we’ve looked at DDD Entities, that have state, and Events, where state changes. To reduce complexity we can be specific about what has state and encapsulate where it changes. Events are high level code, sitting in the middle of the Onion Architecture. In this article, we will look at how lower level commands link the user interface or API with events to enable users to change state.
Onion Architecture
Domain events exist in the high level Domain centre of an Onion Architecture diagram, covered by Robert C. Martin in Clean Architecture. The outer layer of the onion contains low level details, inputs and outputs such as storage, user Interfaces, and APIs. Commands are somewhere in between, linking the inputs to the higher level domain so that users can trigger the events to change state.
Introducing CQRS
For a user to interact with a system, and for it to be useful, we need to be able to do two things. First is to display information to the user. To do that we need to read the…