Aggregate
Abstract base for aggregates.
An aggregate is the main building block of a business model. Aggregates guarantee consistency of data modifications in response to commands they receive.
An aggregate modifies its state in response to a command and produces one or more events. These events are used later to restore the state of the aggregate.
Creating an aggregate class
To create a new aggregate class:
- Select a type for identifiers of the aggregate. If you select to use a typed identifier (which is recommended), define a protobuf message for the ID type.
- Define the structure of the aggregate state as a Protobuf message.
- Generate Java code for ID and state types.
- Create new Java class derived from
Aggregate
passing ID and state types as generic parameters.
Adding command handler methods
Command handling methods of an Aggregate
are defined in the same way as described in CommandHandlingEntity.
Event(s) returned by command handling methods are posted to the EventBus automatically by AggregateRepository.
Adding event applier methods
Aggregate data is stored as a sequence of events it produces. The state of the aggregate is restored by re-playing the history of events and invoking corresponding event applier methods.
An event applier is a method that changes the state of the aggregate in response to an event. An event applier takes a single parameter of the event message it handles and returns void
.
The modification of the state is done using a builder instance obtained from builder. All changes to state become reflected in state()
, after all events (obtained from aggregate's history when loading an aggregate, or emitted by command handlers during the command dispatching) are played.
End-users must not call state()
method within an event applier. It is so, because event appliers are invoked in scope of an active transaction, which accumulates the model updates in aggregate's builder()
, and not in state()
. Therefore, state()
invocation from the applier's code may return some inconsistent result, and in general is prone to errors. All such attempts will result in a RuntimeException
.
An Aggregate
class must have applier methods for all types of the events that it produces.
Performance considerations
To improve performance of loading aggregates, an AggregateRepository periodically stores aggregate snapshots. See setSnapshotTrigger for details.
Parameters
the type for IDs of this class of aggregates
the type of the state held by the aggregate
the type of the aggregate state builder
Inheritors
Constructors
Functions
Inherited functions
archived
.deleted
.ValueMismatch
for the case of discovering a non-default value when the default value was expected by a command.ValueMismatch
for the case of discovering a non-empty value, when an empty string was expected by a command.ValueMismatch
for a command that wanted to clear a value, but discovered that the field already has the default value.ValueMismatch
for a command that wanted to change a field value, but discovered that the field has the default value.ValueMismatch
for a command that wanted to clear a string value but discovered that the field is already empty.EventPlayer
for the given entity.ValueMismatch
for the case of discovering a value different than by a command.ValueMismatch
for the case of discovering a value different than expected by a command.