org.axonframework.repository
Class AbstractRepository<T extends AggregateRoot>

java.lang.Object
  extended by org.axonframework.repository.AbstractRepository<T>
Type Parameters:
T - The type of aggregate this repository stores
All Implemented Interfaces:
Repository<T>
Direct Known Subclasses:
LockingRepository

public abstract class AbstractRepository<T extends AggregateRoot>
extends Object
implements Repository<T>

Abstract implementation of the Repository that takes care of the dispatching of events when an aggregate is persisted. All uncommitted events on an aggregate are dispatched when the aggregate is saved.

Note that this repository implementation does not take care of any locking. The underlying persistence is expected to deal with concurrency. Alternatively, consider using the LockingRepository.

Since:
0.1
Author:
Allard Buijze
See Also:
setEventBus(org.axonframework.eventhandling.EventBus), LockingRepository

Constructor Summary
protected AbstractRepository(Class<T> aggregateType)
          Initializes a repository that stores aggregate of the given aggregateType.
 
Method Summary
 void add(T aggregate)
          Adds the given aggregate to the repository.
protected abstract  void doDelete(T aggregate)
          Removes the aggregate from the repository.
protected abstract  T doLoad(Object aggregateIdentifier, Long expectedVersion)
          Loads and initialized the aggregate with the given aggregateIdentifier.
protected abstract  void doSave(T aggregate)
          Performs the actual saving of the aggregate.
protected  Class<T> getAggregateType()
          Returns the aggregate type stored by this repository.
 T load(Object aggregateIdentifier)
          Load the aggregate with the given unique identifier.
 T load(Object aggregateIdentifier, Long expectedVersion)
          Load the aggregate with the given unique aggregateIdentifier, expecting the version of the aggregate to be equal to the given expectedVersion.
protected  void postDelete(T aggregate)
          Perform action that needs to be done directly after deleting an aggregate and committing the aggregate's uncommitted events.
protected  void postSave(T aggregate)
          Perform action that needs to be done directly after updating an aggregate and committing the aggregate's uncommitted events.
 void setEventBus(EventBus eventBus)
          Sets the event bus to which newly stored events should be published.
protected  void validateOnLoad(T aggregate, Long expectedVersion)
          Checks the aggregate for concurrent changes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractRepository

protected AbstractRepository(Class<T> aggregateType)
Initializes a repository that stores aggregate of the given aggregateType. All aggregates in this repository must be instanceOf this aggregate type.

Parameters:
aggregateType - The type of aggregate stored in this repository
Method Detail

add

public void add(T aggregate)
Adds the given aggregate to the repository. The version of this aggregate must be null, indicating that it has not been previously persisted.

This method will not force the repository to save the aggregate immediately. Instead, it is registered with the current UnitOfWork. To force storage of an aggregate, commit the current unit of work (CurrentUnitOfWork.commit())

Specified by:
add in interface Repository<T extends AggregateRoot>
Parameters:
aggregate - The aggregate to add to the repository.

load

public T load(Object aggregateIdentifier,
              Long expectedVersion)
Load the aggregate with the given unique aggregateIdentifier, expecting the version of the aggregate to be equal to the given expectedVersion. If the expectedVersion is null, no version validation is done.

When versions do not match, implementations may either raise an exception immediately when loading an aggregate, or at any other time while the aggregate is registered in the current Unit Of Work.

Specified by:
load in interface Repository<T extends AggregateRoot>
Parameters:
aggregateIdentifier - The identifier of the aggregate to load
expectedVersion - The expected version of the aggregate to load, or null to indicate the version should not be checked
Returns:
The aggregate root with the given identifier.
Throws:
AggregateNotFoundException - if aggregate with given id cannot be found
RuntimeException - any exception thrown by implementing classes
See Also:
UnitOfWork

load

public T load(Object aggregateIdentifier)
Load the aggregate with the given unique identifier. No version checks are done when loading an aggregate, meaning that concurrent access will not be checked for.

Specified by:
load in interface Repository<T extends AggregateRoot>
Parameters:
aggregateIdentifier - The identifier of the aggregate to load
Returns:
The aggregate root with the given identifier.

validateOnLoad

protected void validateOnLoad(T aggregate,
                              Long expectedVersion)
Checks the aggregate for concurrent changes. Throws a ConflictingModificationException when conflicting changes have been detected.

This implementation throws a ConflictingAggregateVersionException if the expected version is not null and the version number of the aggregate does not match the expected version

Parameters:
aggregate - The loaded aggregate
expectedVersion - The expected version of the aggregate
Throws:
ConflictingModificationException
ConflictingAggregateVersionException

getAggregateType

protected Class<T> getAggregateType()
Returns the aggregate type stored by this repository.

Returns:
the aggregate type stored by this repository

doSave

protected abstract void doSave(T aggregate)
Performs the actual saving of the aggregate.

Parameters:
aggregate - the aggregate to store

doLoad

protected abstract T doLoad(Object aggregateIdentifier,
                            Long expectedVersion)
Loads and initialized the aggregate with the given aggregateIdentifier.

Parameters:
aggregateIdentifier - the identifier of the aggregate to load
expectedVersion - The expected version of the aggregate to load
Returns:
a fully initialized aggregate
Throws:
AggregateNotFoundException - if the aggregate with given identifier does not exist

doDelete

protected abstract void doDelete(T aggregate)
Removes the aggregate from the repository. Typically, the repository should ensure that any calls to doLoad(Object, Long) throw a AggregateNotFoundException when loading a deleted aggregate.

Parameters:
aggregate - the aggregate to delete

setEventBus

public void setEventBus(EventBus eventBus)
Sets the event bus to which newly stored events should be published.

Parameters:
eventBus - the event bus to publish events to

postSave

protected void postSave(T aggregate)
Perform action that needs to be done directly after updating an aggregate and committing the aggregate's uncommitted events.

Parameters:
aggregate - The aggregate instance being saved

postDelete

protected void postDelete(T aggregate)
Perform action that needs to be done directly after deleting an aggregate and committing the aggregate's uncommitted events.

Parameters:
aggregate - The aggregate instance being saved


Copyright © 2010-2016. All Rights Reserved.