Skip to content

Conversation

gtoselli
Copy link
Contributor

@gtoselli gtoselli commented Sep 10, 2024

Having the handler wrapped with a "context manager" allow us to some magical stuff  🪄

Example: transactional context

  1. Api controller send (sendSync) a submitOrderCommand on the local command bus
  2. A fresh context is initialised (is passed to the handler) and the transaction BEGIN
  3. The SubmitOrderCommandHandler run some business logic and persist the aggregate (or whatever) using passed transaction (something like await this.repo.save(aggregate, context.transaction); )
  4. The SubmitOrderCommandHandler publish an OrderSubmittedEvent on a local event bus (using publishAndWaitForHandlers). The event is local, so in the payload, in addition to the business data, the context of the command is also put
  5. The OrderSubmittedEventHandler will probably send another command (something like await this.commandBus.send(command, payload.context);
  6. The context manager sees that there is a transaction and passes it to the handler without creating another one
  7. Since everything is awaited, when the last handler in the chain slowly resolves everyone and when the first one resolves, the transaction is committed (or rollback if the last handler reject)

@gtoselli gtoselli linked an issue Sep 10, 2024 that may be closed by this pull request
Copy link

codecov bot commented Sep 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.93%. Comparing base (07cf236) to head (970ed55).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #66      +/-   ##
==========================================
+ Coverage   89.84%   89.93%   +0.09%     
==========================================
  Files          24       24              
  Lines         443      447       +4     
  Branches       74       79       +5     
==========================================
+ Hits          398      402       +4     
  Misses         45       45              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gtoselli gtoselli self-assigned this Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Local command bus context

1 participant