Domain events that trigger other domain changes in CQRS
Asked Answered
M

1

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"
Metage answered 6/1, 2012 at 20:41 Comment(0)
C
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.

Cuyp answered 6/1, 2012 at 20:49 Comment(2)
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. blog.jonathanoliver.com/2010/09/…Metage
@TrystanSpangler That link gives a 404 and seems to have been relocated to blog.jonathanoliver.com/…Kinesthesia

© 2022 - 2024 — McMap. All rights reserved.