1

General question: Who is responsible for handling events included in a single command?

Simplified example

Two aggregates are given: User and Game.

To start a Game with the StartGame-command, both Users have to accept. In terms of my domain-driven solution this results in the following event-flow: GameRequested -> UserAccepted or UserRejected -> GameStarted or GameFailed.

This event flow and the corresponding business logic code is part of the domain, hence I want to stipulate that behavior in the domain layer. But often I see that the application layer does all of that event handling and executing behavior on the aggregates to bridge the gap. Is the application layer really responsible for determining which aggregate has to be called (and how) next for proceeding this GameStart-command procedure (spanning multiple aggregates and events)?

Luca Nate Mahler
  • 1,292
  • 2
  • 13
  • 28
  • Have a rethink about your aggregates, maybe you're modelling the wrong thing. I can see scenarios where the Game aggregate has within it several Players. Players who are also Users. – Naeem Sarfraz Jun 15 '17 at 11:57
  • @NaeemSarfraz Have a look at my original question: https://stackoverflow.com/questions/44413766/enforce-strict-consistency-spanning-multiple-aggregates This is my main problem I could not solve in a way I think is appropriate. – Luca Nate Mahler Jun 15 '17 at 12:26

1 Answers1

1

Is the application layer really responsible for determining which aggregate has to be called (and how) next for proceeding this GameStart-command procedure (spanning multiple aggregates and events)?

Yes, but it doesn't come up that often.

The basic premise is that -- from the point of view of the domain model, each command modifies one only one aggregate. Those commands typically exist within the confines of a single transaction.

Now, that said, there are a couple of patterns where a single message being handled in the application component may need to coordinate multiple commands with the domain model.

One reason for doing this might be a compound user interface - the application takes a single message (FormSubmitted, HttpRequestReceived), and translates that message as an assortment of commands invoked against different services. See Udi Dahan's Putting your events on a diet.

Another reason that this might happen is if you are trying to maintain backwards compatibility with a previous api that used to handle the message within a single aggregate, and now does so in two.

One way of achieving this is to use a state machine; "process manager" is a term that you will sometimes find in the literature. It's just a buzzwordy term for a protocol that you use to track what has happened, and what needs to happen next.

VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • Do you have an approach in mind for solving this problem that multiple aggregates constitute a unit for a while, entailing that they aren't available in the meantime? It would be nice to have just one aggregate involved in this command. – Luca Nate Mahler Jun 16 '17 at 06:50
  • https://stackoverflow.com/questions/44413766/enforce-strict-consistency-spanning-multiple-aggregates This is the main problem I face. – Luca Nate Mahler Jun 16 '17 at 06:51