I- the type for IDs of this class of aggregates
S- the type of the state held by the aggregate
B- the type of the aggregate state builder
public abstract class Aggregate<I,S extends com.google.protobuf.Message,B extends io.spine.validate.ValidatingBuilder<S,? extends com.google.protobuf.Message.Builder>> extends CommandHandlingEntity<I,S,B> implements EventReactor
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.
In order to create a new aggregate class you need to:
Aggregatepassing ID and state types as generic parameters.
Command handling methods of an
Aggregate are defined in
the same way as described in
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
The modification of the state is done via a builder instance obtained
Aggregate class must have applier methods for
all types of the events that it produces.
|Modifier||Constructor and Description|
Creates a new instance.
|Modifier and Type||Method and Description|
Clears recent history.
Obtains a method for the passed command and invokes it.
Obtains the instance of the state builder.
Instructs to modify the state of an aggregate only within an event applier method.
Creates an iterator of the aggregate event history with reverse traversal.
Obtains model class for this aggregate.
Obtains the version number of the entity.
expectedDefault, expectedEmpty, expectedNotDefault, expectedNotDefault, expectedNotEmpty, getProducerId, unexpectedValue, unexpectedValue
getLifecycleFlags, recentHistory, remember, setArchived, setDeleted, setInitialState, tx
checkEntityState, checkNotArchived, checkNotDeleted, equals, getDefaultState, getId, getState, getVersion, hashCode, idAsString, isArchived, isDeleted, lifecycleFlagsChanged, toString, whenModified
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
protected Aggregate(I id)
id- the ID for the new aggregate
protected AggregateClass<?> thisClass()
protected B getBuilder()
This method must be called only from within an active transaction.
Aggregate, this method must be called only from within an event applier.
TransactionalEntity<I,S extends com.google.protobuf.Message,B extends io.spine.validate.ValidatingBuilder<S,? extends com.google.protobuf.Message.Builder>>
java.lang.IllegalStateException- if the method is called from outside an event applier
protected java.util.List<io.spine.core.Event> dispatchCommand(io.spine.core.CommandEnvelope command)
Dispatching the commands results in emitting event messages. All the empty messages are filtered out from the result.
CommandHandlingEntity<I,S extends com.google.protobuf.Message,B extends io.spine.validate.ValidatingBuilder<S,? extends com.google.protobuf.Message.Builder>>
command- the envelope with the command to dispatch
public void play(java.lang.Iterable<io.spine.core.Event> events)
protected java.lang.String getMissingTxMessage()
protected void clearRecentHistory()
Opens the method for the repository.
protected java.util.Iterator<io.spine.core.Event> historyBackward()
The records are returned sorted by timestamp in a descending order (from newer to older).
The iterator is empty if there's no history for the aggregate.