Domain events that trigger other domain changes in CQRS

2.2k views Asked by At

In all the examples of CQRS I've seen, the domain events trigger updates to the read model but nothing else. But what about when you want a domain event to cause other changes in the domain?

For example, assume you have the following requirements:

  • when the "close account" button is clicked, close the account
  • when the account is payed off, close the account
  • when an account is closed, mark the account owner as "special"

What's the best way to handle this?

  1. Make Account.Close() create a AccountClosed event and also mark the owner as "special"
  2. Make an AccountClosed handler that marks the owner as "special"
  3. Make an AccountClosed handler that submit a MarkOwnerAsSpecial command
  4. Make the command handlers that close the account also mark the account owner as "special"
1

There are 1 answers

2
Dennis Traub On BEST ANSWER

There's a specific concept called Sagas for this exact purpose. Start with this article by Rinat Abdullin, then go from there.

https://blog.jonathanoliver.com/cqrs-sagas-with-event-sourcing-part-i-of-ii/

Your option 3 comes close to this concept. A saga is basically an event handler that issues new commands. You wouldn't want to have an event manipulate aggregates outside the one it originates from but rather handle the event and submit new commands according to your business rules. This is what the saga will do.