org.axonframework.eventsourcing
Class AbstractEventSourcedAggregateRoot

java.lang.Object
  extended by org.axonframework.domain.AbstractAggregateRoot
      extended by org.axonframework.eventsourcing.AbstractEventSourcedAggregateRoot
All Implemented Interfaces:
Serializable, AggregateRoot, EventSourcedAggregateRoot
Direct Known Subclasses:
AbstractAnnotatedAggregateRoot

public abstract class AbstractEventSourcedAggregateRoot
extends AbstractAggregateRoot
implements EventSourcedAggregateRoot

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
protected AbstractEventSourcedAggregateRoot()
          Initializes the aggregate root using a random aggregate identifier.
protected AbstractEventSourcedAggregateRoot(AggregateIdentifier identifier)
          Initializes the aggregate root using the provided aggregate identifier.
protected AbstractEventSourcedAggregateRoot(UUID identifier)
          Deprecated. Use AbstractEventSourcedAggregateRoot(org.axonframework.domain.AggregateIdentifier). Will be removed in next release.
 
Method Summary
protected  void apply(DomainEvent event)
          Apply the provided event.
protected  Collection<AbstractEventSourcedEntity> getChildEntities()
          Returns a list of event sourced entities directly referenced by the aggregate root.
protected abstract  void handle(DomainEvent 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.
 
Methods inherited from class org.axonframework.domain.AbstractAggregateRoot
commitEvents, getIdentifier, getUncommittedEventCount, getUncommittedEvents, getVersion, initializeEventStream, registerEvent
 
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
commitEvents, getIdentifier, getUncommittedEventCount, getUncommittedEvents, getVersion
 

Constructor Detail

AbstractEventSourcedAggregateRoot

protected AbstractEventSourcedAggregateRoot()
Initializes the aggregate root using a random aggregate identifier.


AbstractEventSourcedAggregateRoot

protected AbstractEventSourcedAggregateRoot(AggregateIdentifier identifier)
Initializes the aggregate root using the provided aggregate identifier.

Parameters:
identifier - the identifier of this aggregate

AbstractEventSourcedAggregateRoot

@Deprecated
protected AbstractEventSourcedAggregateRoot(UUID identifier)
Deprecated. Use AbstractEventSourcedAggregateRoot(org.axonframework.domain.AggregateIdentifier). Will be removed in next release.

Initializes the aggregate root using the provided aggregate identifier.

Parameters:
identifier - the identifier of this aggregate
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 two special types of DomainEvents: The AggregateDeletedEvent, which indicates that the aggregate is deleted and the AggregateSnapshot, which is a snapshot event, containing the actual aggregate inside.

When an AggregateDeletedEvent is encountered, a AggregateDeletedException is thrown, unless there are events following the AggregateDeletedEvent. This could be the case when an event is added to the stream as a correction to an earlier event.

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

Specified by:
initializeState in interface EventSourcedAggregateRoot
Parameters:
domainEventStream - the event stream containing the events that describe the state changes of this aggregate
Throws:
IllegalStateException - if this aggregate was already initialized.
AggregateDeletedException - if the event stream contains an event of type AggregateDeletedEvent (or subtype).

apply

protected void apply(DomainEvent event)
Apply the provided event. Applying events means they are added to the uncommitted event queue and forwarded to the handle(DomainEvent) event handler method} for processing.

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

Parameters:
event - The event to apply

getChildEntities

protected Collection<AbstractEventSourcedEntity> getChildEntities()
Returns a list of event sourced entities directly referenced by the aggregate root.

The default implementation uses reflection to find references to AbstractEventSourcedEntity implementations.

It will look for entities:

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

handle

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

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

Parameters:
event - The event to handle


Copyright © 2011. All Rights Reserved.