Interface ApplyMore
- All Known Implementing Classes:
AnnotatedAggregate,EventSourcedAggregate
The most common application of this mechanism is in the event handler method of an aggregate. Say that an event handler for an event of type A applies another event of type B and then an event of type C. Only after the processing of event A is finished will event B be applied to the aggregate. That means that the effects of event B will not be visible from within the event handler. This is a problem if event C depends on the changes caused by event B. The ApplyMore mechanism solves this problem by allowing clients to apply additional events after previous events have been fully processed.
Be careful not to make any direct changes to the aggregate from the supplier of the subsequent event. If the aggregate is event sourced this will cause the aggregate to be in an inconsistent state after event sourcing.
-
Method Summary
Modifier and TypeMethodDescriptionExecute the givenrunnableafter applying the previous event.andThenApply(Supplier<?> payloadOrMessageSupplier) Apply a subsequent event to the aggregate after applying another event.default ApplyMoreandThenApplyIf(Supplier<Boolean> condition, Supplier<?> payloadOrMessageSupplier) Conditionally apply a subsequent event to the aggregate after applying another event.default ApplyMoreConditionally execute the givenrunnableafter applying the previous event.
-
Method Details
-
andThenApply
Apply a subsequent event to the aggregate after applying another event. When the givenpayloadOrMessageSupplieris asked to provide the subsequent event the initial event has been fully processed by the aggregate.If the given supplier passes an object that is an instance of a
Messagethe event is applied with the metadata from the message. If the supplied event is not a Message instance it will be applied as an event without additional metadata.- Parameters:
payloadOrMessageSupplier- The next event message or the payload of the next event- Returns:
- an instance of ApplyMore to apply any subsequent events
-
andThenApplyIf
Conditionally apply a subsequent event to the aggregate after applying another event. When the givenpayloadOrMessageSupplieris asked to provide the subsequent event the initial event has been fully processed by the aggregate.If the given supplier passes an object that is an instance of a
Messagethe event is applied with the metadata from the message. If the supplied event is not a Message instance it will be applied as an event without additional metadata.- Parameters:
condition- the condition to evaluate and decide if the payload should be appliedpayloadOrMessageSupplier- The next event message or the payload of the next event- Returns:
- an instance of ApplyMore to apply any subsequent events
-
andThen
Execute the givenrunnableafter applying the previous event. Thisrunnableis guaranteed to be invoked when the previous event has been fully applied to the aggregate.The given
runnablemust not directly alter any state of the aggregate. Instead, it should only decide if more events should be applied based on the state of the aggregate after the previous event- Parameters:
runnable- the code to execute when the previous event was applied- Returns:
- an instance of ApplyMore to apply any subsequent events
-
andThenIf
Conditionally execute the givenrunnableafter applying the previous event. Thisrunnableis guaranteed to be invoked when the previous event has been fully applied to the aggregate.The given
runnablemust not directly alter any state of the aggregate. Instead, it should only decide if more events should be applied based on the state of the aggregate after the previous event- Parameters:
condition- the condition to evaluate and decide if the code should be executedrunnable- the code to execute when the previous event was applied- Returns:
- an instance of ApplyMore to apply any subsequent events
-