Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain events that trigger other domain changes in CQRS

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"
like image 325
Trystan Spangler Avatar asked Jan 06 '12 20:01

Trystan Spangler


1 Answers

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.

like image 173
Dennis Traub Avatar answered Sep 17 '22 23:09

Dennis Traub