org.axonframework.eventsourcing
Class AbstractEventSourcedAggregateRoot<I>

java.lang.Object
  extended by org.axonframework.domain.AbstractAggregateRoot<I>
      extended by org.axonframework.eventsourcing.AbstractEventSourcedAggregateRoot<I>
Type Parameters:
I - The type of the identifier of this aggregate
All Implemented Interfaces:
Serializable, AggregateRoot<I>, EventSourcedAggregateRoot<I>
Direct Known Subclasses:
AbstractAnnotatedAggregateRoot, ToDoItem

@MappedSuperclass
public abstract class AbstractEventSourcedAggregateRoot<I>
extends AbstractAggregateRoot<I>
implements EventSourcedAggregateRoot<I>

Abstract convenience class to be extended by all aggregate roots. The AbstractEventSourcedAggregateRoot tracks all uncommitted events. It also provides convenience methods to initialize the state of the aggregate root based on a DomainEventStream, which can be used for event sourcing.

Since:
0.1
Author:
Allard Buijze
See Also:
Serialized Form

Constructor Summary
AbstractEventSourcedAggregateRoot()
           
 
Method Summary
protected  void apply(Object eventPayload)
          Apply the provided event.
protected  void apply(Object eventPayload, MetaData metaData)
          Apply the provided event.
 void commitEvents()
          Clears the events currently marked as "uncommitted" and clears any known EventRegistrationCallbacks (see AggregateRoot.addEventRegistrationCallback(EventRegistrationCallback)).
protected abstract  Iterable<? extends EventSourcedEntity> getChildEntities()
          Returns a collection of event sourced entities directly referenced by this entity.
 Long getVersion()
          Returns the current version number of the aggregate, or null if the aggregate is newly created.
protected abstract  void handle(DomainEventMessage event)
          Apply state changes based on the given event.
 void initializeState(DomainEventStream domainEventStream)
          Initialize the state of this aggregate using the events in the provided DomainEventStream.
protected  boolean isLive()
          Indicates whether this aggregate is in "live" mode.
 
Methods inherited from class org.axonframework.domain.AbstractAggregateRoot
addEventRegistrationCallback, getLastCommittedEventSequenceNumber, getUncommittedEventCount, getUncommittedEvents, initializeEventStream, isDeleted, markDeleted, registerEvent, registerEvent, registerEventMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.axonframework.domain.AggregateRoot
addEventRegistrationCallback, getIdentifier, getUncommittedEventCount, getUncommittedEvents, isDeleted
 

Constructor Detail

AbstractEventSourcedAggregateRoot

public AbstractEventSourcedAggregateRoot()
Method Detail

initializeState

public void initializeState(DomainEventStream domainEventStream)
Initialize the state of this aggregate using the events in the provided DomainEventStream. A call to this method on an aggregate that has already been initialized will result in an IllegalStateException.

This implementation is aware of a special type of DomainEvents: the AggregateSnapshot, which is a snapshot event, containing the actual aggregate inside.

AggregateSnapshot events are used to initialize the aggregate with the correct version (getVersion()).

Specified by:
initializeState in interface EventSourcedAggregateRoot<I>
Parameters:
domainEventStream - the event stream containing the events that describe the state changes of this aggregate
Throws:
IllegalStateException - if this aggregate was already initialized.

apply

protected void apply(Object eventPayload)
Apply the provided event. Applying events means they are added to the uncommitted event queue and forwarded to the handle(org.axonframework.domain.DomainEventMessage) event handler method} for processing.

The event is applied on all entities part of this aggregate.

Parameters:
eventPayload - The payload of the event to apply

apply

protected void apply(Object eventPayload,
                     MetaData metaData)
Apply the provided event. Applying events means they are added to the uncommitted event queue and forwarded to the handle(org.axonframework.domain.DomainEventMessage) event handler method} for processing.

The event is applied on all entities part of this aggregate.

Parameters:
eventPayload - The payload of the event to apply
metaData - any meta-data that must be registered with the Event

commitEvents

public void commitEvents()
Description copied from class: AbstractAggregateRoot
Clears the events currently marked as "uncommitted" and clears any known EventRegistrationCallbacks (see AggregateRoot.addEventRegistrationCallback(EventRegistrationCallback)).

Specified by:
commitEvents in interface AggregateRoot<I>
Overrides:
commitEvents in class AbstractAggregateRoot<I>
See Also:
EventContainer.commit()

isLive

protected boolean isLive()
Indicates whether this aggregate is in "live" mode. This is the case when an aggregate is fully initialized and ready to handle commands.

Typically, this method is used to check the state of the aggregate while events are being handled. When the aggregate is handling an event to reconstruct its current state, isLive() returns false. If an event is being handled because is was applied as a result of the current command being executed, it returns true.

isLive() can be used to prevent expensive calculations while event sourcing.

Returns:
true if the aggregate is live, false when the aggregate is relaying historic events.

getChildEntities

protected abstract Iterable<? extends EventSourcedEntity> getChildEntities()
Returns a collection of event sourced entities directly referenced by this entity. May return null or an empty list to indicate no child entities are available. The collection may also contain null values.

Events are propagated to the children in the order that the iterator of the return value provides.

Returns:
a list of event sourced entities contained in this aggregate

handle

protected abstract void handle(DomainEventMessage event)
Apply state changes based on the given event.

Note: Implementations of this method should *not* perform validation.

Parameters:
event - The event to handle

getVersion

public Long getVersion()
Description copied from class: AbstractAggregateRoot
Returns the current version number of the aggregate, or null if the aggregate is newly created. This version must reflect the version number of the aggregate on which changes are applied.

Each time the aggregate is modified and stored in a repository, the version number must be increased by at least 1. This version number can be used by optimistic locking strategies and detection of conflicting concurrent modification.

Typically the sequence number of the last committed event on this aggregate is used as version number.

Specified by:
getVersion in interface AggregateRoot<I>
Overrides:
getVersion in class AbstractAggregateRoot<I>
Returns:
the current version number of this aggregate, or null if no events were ever committed


Copyright © 2010-2016. All Rights Reserved.