Skip to content

Building OpenCQRS

Beyond the Aggregate Root: How OpenCQRS Decouples What the Aggregate Bundled Together

This article is about a part of OpenCQRS that took me a while to grasp, not because the mechanics are hard but because an old modeling habit was in the way. The best way in is a small example. Take a shared bank account: several users can have access to the same account, and one command grants a user that access. It is a few checks and one event, and it is the fastest way to run into a question that a Domain-Driven Design background answers for you and OpenCQRS deliberately does not.

Think about what an access grant is in that domain. It carries rights that change over time, it can be revoked, and when money goes missing you want to know who granted which rights to whom and when. A flag on the account would hold none of that. A grant has its own life and its own history.

The rule for creating one, though, does not belong to the grant. Whether user-99 may get access to account 4711 depends on the account: it has to exist, and it must not be closed. Nothing about the grant itself can answer that. The thing you check and the thing you create are two different things, and no framework is involved yet.

Now put this into OpenCQRS. Every event is stored under a subject, a path like /account/4711. A grant with its own history gets its own subject below the account, /account/4711/access/user-99, and that is where the AccessGrantedEvent belongs. Every command has exactly one subject as well, and before the handler runs, the framework loads the events under that subject and rebuilds the state the handler decides on. So which subject should the grant command have: the one where the event ends up, or the one whose state the rules need?

If you know Domain-Driven Design, you know why this question seems to need exactly one answer. There, the Aggregate Root settles it: the events you read for a decision, the new events you write and the consistency check all belong to the same Aggregate. There is a second way to look at it, which treats those three jobs as separate decisions that only coincide in the simple case. That is the direction Dynamic Consistency Boundaries take, and it is the direction OpenCQRS takes, with subject paths instead of a query. In this article we look at what the Aggregate Root actually does, why I think the split is the better default, and how the three parts become independent dimensions that each command can shape on its own.