9

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"
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
Trystan Spangler
  • 1,685
  • 11
  • 20

1 Answers1

10

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.

jv-dev
  • 646
  • 7
  • 14
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • I've read that before but it was so abstract that I didn't get how sagas basically turn events into commands. I found another 2 part article that explains that and has code samples. http://blog.jonathanoliver.com/2010/09/cqrs-sagas-with-event-sourcing-part-i-of-ii/ – Trystan Spangler Jan 09 '12 at 16:31
  • @TrystanSpangler That link gives a 404 and seems to have been relocated to http://blog.jonathanoliver.com/cqrs-sagas-with-event-sourcing-part-i-of-ii/ – Melle Nov 27 '17 at 20:04