My team went over this issue a few times and the ‘ah-ha’ moment was when we realised we shouldn’t have our command handlers themselves be responsible for the unit of work themselves. Rather, we start a transaction in the unit of work and then run our command handlers. Then, in situations like yours where you want to react to ‘internal’ events (ie. those in the same BC) synchronously it’s simply a case of calling a command handler, then calling any event handlers which may themselves return further commands (i.e., the process manager pattern) and running those commands’ handlers within the same transaction. This way, no event handlers has to know whether ‘a transaction has already begun’ because they’re not responsible for the Unit of Work.
PS. You didn’t ask this, but another ‘ah-ha’ moment was when we realised that process managers (event handlers which may return commands in response) are part of our domain logic and thus should be part of our domain model. We actually go even further and make commands and command handlers part of the domain model too. The application/infrastructure layers are only responsible for taking commands from user input, or those returned from the domain model’s process managers, and passing them back to the domain model to execute by resolving the appropriate command handler.
This Twitter thread might be useful: https://twitter.com/mathiasverraes/status/1302233693021446150
You received this message because you are subscribed to the Google Groups "DDD/CQRS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dddcqrs+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dddcqrs/8b5cd986-74bd-438e-9bee-923351cddca0n%40googlegroups.com.
On 12 May 2021, at 08:35, Yordis Prieto <yordis...@gmail.com> wrote:
Here is how I see the topic related to Domain Events and Events. Imagine for a moment we are just working in one domain (so a single context):
To view this discussion on the web visit https://groups.google.com/d/msgid/dddcqrs/7ebcb466-9457-4a81-a8a6-63b97223cc77n%40googlegroups.com.