T
- The type of aggregate this repository storespublic abstract class AbstractRepository<T,A extends Aggregate<T>> extends Object implements Repository<T>
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
.
LockingRepository
Modifier | Constructor and Description |
---|---|
protected |
AbstractRepository(AggregateModel<T> aggregateModel)
Initializes a repository that stores aggregate of the given
aggregateType . |
protected |
AbstractRepository(Class<T> aggregateType)
Initializes a repository that stores aggregate of the given
aggregateType . |
protected |
AbstractRepository(Class<T> aggregateType,
ParameterResolverFactory parameterResolverFactory)
Initializes a repository that stores aggregate of the given
aggregateType . |
Modifier and Type | Method and Description |
---|---|
protected AggregateModel<T> |
aggregateModel()
Returns the aggregate model stored by this repository.
|
protected abstract A |
doCreateNew(Callable<T> factoryMethod)
Creates a new aggregate instance using the given
factoryMethod . |
protected abstract void |
doDelete(A aggregate)
Removes the aggregate from the repository.
|
protected abstract A |
doLoad(String aggregateIdentifier,
Long expectedVersion)
Loads and initialized the aggregate with the given aggregateIdentifier.
|
protected abstract void |
doSave(A aggregate)
Performs the actual saving of the aggregate.
|
protected Class<? extends T> |
getAggregateType()
Returns the aggregate type stored by this repository.
|
A |
load(String aggregateIdentifier)
Load the aggregate with the given unique identifier.
|
A |
load(String aggregateIdentifier,
Long expectedVersion)
Load the aggregate with the given unique identifier.
|
protected Map<String,A> |
managedAggregates(UnitOfWork<?> uow)
Returns the map of aggregates currently managed by this repository under the given unit of work.
|
A |
newInstance(Callable<T> factoryMethod)
Creates a new managed instance for the aggregate, using the given
factoryMethod
to instantiate the aggregate's root. |
protected void |
postDelete(A aggregate)
Perform action that needs to be done directly after deleting an aggregate and committing the aggregate's
uncommitted events.
|
protected void |
postSave(A aggregate)
Perform action that needs to be done directly after updating an aggregate and committing the aggregate's
uncommitted events.
|
protected void |
prepareForCommit(A aggregate)
Register handlers with the current Unit of Work that save or delete the given
aggregate when
the Unit of Work is committed. |
protected void |
reportIllegalState(A aggregate)
Invoked when an the given
aggregate instance has been detected that has been part of a rolled back Unit
of Work. |
protected void |
validateOnLoad(Aggregate<T> aggregate,
Long expectedVersion)
Checks the aggregate for concurrent changes.
|
protected AbstractRepository(Class<T> aggregateType)
aggregateType
. All aggregates in this
repository must be instanceOf
this aggregate type.aggregateType
- The type of aggregate stored in this repositoryprotected AbstractRepository(Class<T> aggregateType, ParameterResolverFactory parameterResolverFactory)
aggregateType
. All aggregates in this
repository must be instanceOf
this aggregate type.aggregateType
- The type of aggregate stored in this repositoryparameterResolverFactory
- The parameter resolver factory used to resolve parameters of annotated handlersprotected AbstractRepository(AggregateModel<T> aggregateModel)
aggregateType
. All aggregates in this
repository must be instanceOf
this aggregate type.aggregateModel
- The model describing the structure of the aggregatepublic A newInstance(Callable<T> factoryMethod) throws Exception
Repository
factoryMethod
to instantiate the aggregate's root.newInstance
in interface Repository<T>
factoryMethod
- The method to create the aggregate's root instanceException
- when the factoryMethod throws an exceptionprotected abstract A doCreateNew(Callable<T> factoryMethod) throws Exception
factoryMethod
. Implementations should assume that this
method is only called if a UnitOfWork is currently active.factoryMethod
- The method to create the aggregate's root instanceException
- when the factoryMethod throws an exceptionpublic A load(String aggregateIdentifier, Long expectedVersion)
Repository
load
in interface Repository<T>
aggregateIdentifier
- The identifier of the aggregate to loadexpectedVersion
- The expected version of the loaded aggregateAggregateNotFoundException
- if aggregate with given id cannot be foundRuntimeException
- any exception thrown by implementing classesprotected Map<String,A> managedAggregates(UnitOfWork<?> uow)
The returns map is mutable and reflects any changes made during processing.
uow
- The unit of work to find the managed aggregates forpublic A load(String aggregateIdentifier)
Repository
load
in interface Repository<T>
aggregateIdentifier
- The identifier of the aggregate to loadprotected void validateOnLoad(Aggregate<T> aggregate, Long expectedVersion)
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
aggregate
- The loaded aggregateexpectedVersion
- The expected version of the aggregateConflictingModificationException
- when conflicting changes have been detectedConflictingAggregateVersionException
- the expected version is not null
and the version number of the aggregate does not match the expected versionprotected void prepareForCommit(A aggregate)
aggregate
when
the Unit of Work is committed.aggregate
- The Aggregate to save or delete when the Unit of Work is committedprotected void reportIllegalState(A aggregate)
aggregate
instance has been detected that has been part of a rolled back Unit
of Work. This typically means that the state of the Aggregate instance has been compromised and cannot be
guaranteed to be correct.
This implementation throws an exception, effectively causing the unit of work to be rolled back. Subclasses that can guarantee correct storage, even when specific instances are compromised, may override this method to suppress this exception.
When this method is invoked, the doSave(Aggregate)
, doDelete(Aggregate)
,
postSave(Aggregate)
and postDelete(Aggregate)
are not invoked. Implementations may choose to
invoke these methods.
aggregate
- The aggregate instance with illegal stateprotected AggregateModel<T> aggregateModel()
protected Class<? extends T> getAggregateType()
protected abstract void doSave(A aggregate)
aggregate
- the aggregate to storeprotected abstract A doLoad(String aggregateIdentifier, Long expectedVersion)
aggregateIdentifier
- the identifier of the aggregate to loadexpectedVersion
- The expected version of the aggregate to loadAggregateNotFoundException
- if the aggregate with given identifier does not existprotected abstract void doDelete(A aggregate)
doLoad(String, Long)
throw a AggregateNotFoundException
when
loading a deleted aggregate.aggregate
- the aggregate to deleteprotected void postSave(A aggregate)
aggregate
- The aggregate instance being savedprotected void postDelete(A aggregate)
aggregate
- The aggregate instance being savedCopyright © 2010–2017. All rights reserved.