org.axonframework.domain
Class AbstractAggregateRoot<I>

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

@MappedSuperclass
public abstract class AbstractAggregateRoot<I>
extends Object
implements AggregateRoot<I>, Serializable

Very basic implementation of the AggregateRoot interface. It provides the mechanism to keep track of uncommitted events and maintains a version number based on the number of events generated.

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

Constructor Summary
AbstractAggregateRoot()
           
 
Method Summary
 void addEventRegistrationCallback(EventRegistrationCallback eventRegistrationCallback)
          Adds an EventRegistrationCallback, which is notified when the aggregate registers an Event for publication.
 void commitEvents()
          Clears the events currently marked as "uncommitted" and clears any known EventRegistrationCallbacks (see AggregateRoot.addEventRegistrationCallback(EventRegistrationCallback)).
protected  Long getLastCommittedEventSequenceNumber()
          Returns the sequence number of the last committed event, or null if no events have been committed before.
 int getUncommittedEventCount()
          Returns the number of uncommitted events currently available in the aggregate.
 DomainEventStream getUncommittedEvents()
          Returns a DomainEventStream to the events in the aggregate that have been raised since creation or the last commit.
 Long getVersion()
          Returns the current version number of the aggregate, or null if the aggregate is newly created.
protected  void initializeEventStream(long lastSequenceNumber)
          Initialize the event stream using the given sequence number of the last known event.
 boolean isDeleted()
          Indicates whether this aggregate has been marked as deleted.
protected  void markDeleted()
          Marks this aggregate as deleted, instructing a Repository to remove that aggregate at an appropriate time.
protected
<T> DomainEventMessage<T>
registerEvent(MetaData metaData, T payload)
          Registers an event to be published when the aggregate is saved.
protected
<T> DomainEventMessage<T>
registerEvent(T payload)
          Registers an event to be published when the aggregate is saved, containing the given payload and no (additional) meta-data.
protected
<T> DomainEventMessage<T>
registerEventMessage(DomainEventMessage<T> message)
          Registers an event message to be published when the aggregate is saved.
 
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
getIdentifier
 

Constructor Detail

AbstractAggregateRoot

public AbstractAggregateRoot()
Method Detail

registerEvent

protected <T> DomainEventMessage<T> registerEvent(T payload)
Registers an event to be published when the aggregate is saved, containing the given payload and no (additional) meta-data.

Type Parameters:
T - The type of payload
Parameters:
payload - the payload of the event to register
Returns:
The Event holding the given payload

registerEvent

protected <T> DomainEventMessage<T> registerEvent(MetaData metaData,
                                                  T payload)
Registers an event to be published when the aggregate is saved.

Type Parameters:
T - The type of payload
Parameters:
metaData - The meta data of the event to register
payload - the payload of the event to register
Returns:
The Event holding the given payload

registerEventMessage

protected <T> DomainEventMessage<T> registerEventMessage(DomainEventMessage<T> message)
Registers an event message to be published when the aggregate is saved.

Type Parameters:
T - The payload type carried by the message
Parameters:
message - The message to publish
Returns:
The event as registered by the aggregate

markDeleted

protected void markDeleted()
Marks this aggregate as deleted, instructing a Repository to remove that aggregate at an appropriate time.

Note that different Repository implementation may react differently to aggregates marked for deletion. Typically, Event Sourced Repositories will ignore the marking and expect deletion to be provided as part of Event information.


isDeleted

public boolean isDeleted()
Description copied from interface: AggregateRoot
Indicates whether this aggregate has been marked as deleted. When true, it is an instruction to the repository to remove this instance at an appropriate time.

Repositories should not return any instances of Aggregates that return true on isDeleted().

Specified by:
isDeleted in interface AggregateRoot<I>
Returns:
true if this aggregate was marked as deleted, otherwise false.

addEventRegistrationCallback

public void addEventRegistrationCallback(EventRegistrationCallback eventRegistrationCallback)
Description copied from interface: AggregateRoot
Adds an EventRegistrationCallback, which is notified when the aggregate registers an Event for publication. These callbacks are cleared when the aggregate is committed.

If the aggregate contains uncommitted events, they are all passed to the given eventRegistrationCallback for processing.

Specified by:
addEventRegistrationCallback in interface AggregateRoot<I>
Parameters:
eventRegistrationCallback - the callback to notify when an event is registered

getUncommittedEvents

public DomainEventStream getUncommittedEvents()
Returns a DomainEventStream to the events in the aggregate that have been raised since creation or the last commit.

Specified by:
getUncommittedEvents in interface AggregateRoot<I>
Returns:
the DomainEventStream to the uncommitted events.

commitEvents

public void commitEvents()
Clears the events currently marked as "uncommitted" and clears any known EventRegistrationCallbacks (see AggregateRoot.addEventRegistrationCallback(EventRegistrationCallback)).

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

getUncommittedEventCount

public int getUncommittedEventCount()
Returns the number of uncommitted events currently available in the aggregate.

Specified by:
getUncommittedEventCount in interface AggregateRoot<I>
Returns:
the number of uncommitted events currently available in the aggregate.

initializeEventStream

protected void initializeEventStream(long lastSequenceNumber)
Initialize the event stream using the given sequence number of the last known event. This will cause the new events to be attached to this aggregate to be assigned a continuous sequence number.

Parameters:
lastSequenceNumber - The sequence number of the last event from this aggregate

getLastCommittedEventSequenceNumber

protected Long getLastCommittedEventSequenceNumber()
Returns the sequence number of the last committed event, or null if no events have been committed before.

Returns:
the sequence number of the last committed event

getVersion

public Long getVersion()
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>
Returns:
the current version number of this aggregate, or null if no events were ever committed


Copyright © 2010-2016. All Rights Reserved.