SpineEventEngine / core-java

The Java implementation of the framework core
https://spine.io
Apache License 2.0
36 stars 12 forks source link

Prohibit calling `state()` from `Aggregate`'s `@Apply`-er methods #1514

Closed armiol closed 1 year ago

armiol commented 1 year ago

This is a port of #1501 into master branch. Here is a full description of the ported PR.

This changeset addresses #1499 by disallowing to call state() method from aggregate appliers.

Previously, end-users could rely on Aggregate.state() from @Apply-ers. However, in some cases (such as the one described in #1499) state() does not reflect the last state of an aggregate. This is so because appliers are invoked in scope of a single transaction — be it when loading an aggregate instance from storage, or when applying the just-emitted events during the command dispatching. Until such a transaction is completed, state() does not reflect the model updates corresponding to the respective domain events. Using the returned state value most likely leads to bugs.

The designed way has always been to use builder() instead of state(), because it is kept up-to-date throughout the active aggregate transaction.

Therefore, once this PR is merged, any calls to state() from @Apply-annotated method will lead to an IllegalStateException.

The library version is set to 2.0.0-SNAPSHOT.145.

codecov[bot] commented 1 year ago

Codecov Report

Merging #1514 (ef333ea) into delivery-reporting-errors (75b0015) will increase coverage by 0.02%. The diff coverage is 100.00%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## delivery-reporting-errors #1514 +/- ## =============================================================== + Coverage 89.67% 89.70% +0.02% - Complexity 4926 4933 +7 =============================================================== Files 634 635 +1 Lines 15439 15453 +14 Branches 888 888 =============================================================== + Hits 13845 13862 +17 + Misses 1288 1286 -2 + Partials 306 305 -1 ```