Repository navigation
Add before, after and around hooks for applying events - #567
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
An around hook can apply an event twice while recording it once, causing live and replayed state to diverge, and subclasses miss superclass hooks registered after their own hooks.
2 open issues
What changed in this PR
Adds event-application hooks to AggregateRoot so aggregates can check conditions around live state changes without running those checks during replay.
Changes:
- Introduces
before_apply,after_apply, andaround_apply, with method/block callbacks and event matcher filters. - Integrates hooks into live event application while leaving history loading unchanged.
- Adds specifications and documentation for ordering, inheritance, filtering, and failure behavior.
| File | Description |
|---|---|
spec/lib/sequent/core/apply_hooks_spec.rb |
Tests hook behavior, filtering, inheritance, and validation. |
lib/sequent/core/helpers/apply_hooks.rb |
Implements hook registration and execution. |
lib/sequent/core/aggregate_root.rb |
Runs hooks during live event application. |
docs/docs/concepts/aggregate-root.md |
Documents hook usage and failure semantics. |
CHANGELOG.md |
Announces the new hooks and links to documentation. |
Review effort: Balanced
A condition that must hold before and after every change, such as no event touching a closed period, could only be checked by overriding the protected apply and repeating what it does around apply_event. The handler hooks of MessageHandler do not fit, since they also run when an aggregate is loaded, where a period closed later must not stop the events from before it. An around hook that does not apply the event fails rather than leaving an event in the uncommitted events that never reached the state.
An around hook could call its continuation twice. apply_event then ran twice while the event was recorded once, so the state after loading the aggregate differed from the state it had when the event was applied. The second application is now refused before it runs. A subclass that declared a hook held a copy of the inherited hooks, so a hook added to the superclass afterwards, by reopening it or including a module, never ran for it. Each class now keeps its own hooks and combines them with those of its superclass on lookup, cached until a hook is added anywhere, as MessageHandler does for its handler hooks.
erikrozendaal
force-pushed
the
improvement/apply-hooks
branch
from
November 11, 2026 13:48
6be8b02 to
f1d94af
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
A condition that must hold before and after every change of an aggregate, such as no event touching a closed period, can only be checked by overriding the protected
applyand repeating what it does aroundapply_event. The handler hooks ofMessageHandlerdo not fit, since they also run when an aggregate is loaded, where a period closed later must not stop the events from before it.before_apply,after_applyandaround_applyonAggregateRoot, from the newSequent::Core::Helpers::ApplyHooks. They take a method name or a block, which receive the event. An around hook method continues by yielding, an around hook block by calling the continuation it receives.only:andexcept:take the same arguments ason: an event class matches that class only,is_a(SomeEvent)matches its subclasses as well, and other message matchers likehas_attrswork too.apply, not inapply_event, so loading, streaming and loading from a snapshot don't run them.Aggregates without hooks behave as before. An application that overrides
applyitself does not run the hooks until its override does.Tests
spec/lib/sequent/core/apply_hooks_spec.rb:only:andexcept:with classes (exact),is_a(subclasses) andhas_attrs;