Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @invariant decorator to explicitly configure invariants #425

Merged
merged 3 commits into from
May 21, 2024

Conversation

subhashb
Copy link
Contributor

Invariants are usually buried deep in business logic, making them more complex to maintain over time and difficult to document. This PR introduces the @invariant decorator to configure invariants in an aggregate cluster explicitly.

All methods marked @invariant are invoked once an aggregate/entity object is initialized and triggered when an
attribute is updated across the board. Explicit invariants ensure that the entire aggregate cluster remains valid at all times.

Two additional changes are present in this PR:

  1. There will be scenarios where multiple attributes must be changed before the aggregate becomes valid. A new context manager, atomic_change, has been introduced, which temporarily turns off validations and runs them on the exit of the context manager.
  2. All aggregates and entities maintain linkages to their hierarchy, especially their immediate parent and the root aggregate. This linkage helps trigger validations across the aggregate cluster recursively.

subhashb added 3 commits May 20, 2024 11:24
This commit introduces two fields in every entity to track their lineage:
root and owner. The owner is the immediate parent of an entity. This is
the entity (or aggregate) that has the association field. The root is the
aggregate root.

This is the first step to being able to satisfy invariants across an
aggregate's clustes of entities.
Add @invariant decorator, and configure aggregate and element classes
to parse through methods and record them for later use.
When an aggregate or entity object is initialized, or its attribute value
is changed, this commit ensures that all `clean()` methods (which in turn
run all invariant validations) are run across the aggregate.

This commit also adds `atomic_change` context manager for scenarios
where multiple aggregates in an aggregate need to be changed before
it becomes valid again.
@subhashb subhashb self-assigned this May 21, 2024
@subhashb subhashb merged commit 8fcc456 into main May 21, 2024
4 checks passed
@subhashb subhashb deleted the invariant-enhancements branch May 21, 2024 21:03
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.

1 participant