T
- The type of aggregate this repository storespublic class EventSourcingRepository<T> extends LockingRepository<T,EventSourcedAggregate<T>>
EventBus
and delegate event
storage to the provided EventStore
.EventStore
Modifier and Type | Class and Description |
---|---|
static class |
EventSourcingRepository.Builder<T>
Builder class to instantiate a
EventSourcingRepository . |
Modifier | Constructor and Description |
---|---|
protected |
EventSourcingRepository(EventSourcingRepository.Builder<T> builder)
Instantiate a
EventSourcingRepository based on the fields contained in the EventSourcingRepository.Builder . |
Modifier and Type | Method and Description |
---|---|
static <T> EventSourcingRepository.Builder<T> |
builder(Class<T> aggregateType)
Instantiate a Builder to be able to create a
EventSourcingRepository for aggregate type T . |
protected EventSourcedAggregate<T> |
doCreateNewForLock(Callable<T> factoryMethod)
Creates a new aggregate instance using the given
factoryMethod . |
protected void |
doDeleteWithLock(EventSourcedAggregate<T> aggregate)
Perform the actual deleting of the aggregate.
|
protected EventSourcedAggregate<T> |
doLoadWithLock(String aggregateIdentifier,
Long expectedVersion)
Perform the actual loading of an aggregate.
|
protected void |
doSaveWithLock(EventSourcedAggregate<T> aggregate)
Perform the actual saving of the aggregate.
|
AggregateFactory<T> |
getAggregateFactory()
Returns the factory used by this repository.
|
protected DomainEventStream |
readEvents(String aggregateIdentifier)
Reads the events for the given aggregateIdentifier from the eventStore.
|
protected void |
reportIllegalState(LockAwareAggregate<T,EventSourcedAggregate<T>> 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.
|
doCreateNew, doDelete, doLoad, doLoadOrCreate, doSave, prepareForCommit
aggregateModel, canResolve, getAggregateType, load, load, loadOrCreate, managedAggregates, newInstance, postDelete, postSave, send
protected EventSourcingRepository(EventSourcingRepository.Builder<T> builder)
EventSourcingRepository
based on the fields contained in the EventSourcingRepository.Builder
.
A goal of the provided Builder is to create an AggregateModel
specifying generic T
as the
aggregate type to be stored. All aggregates in this repository must be instanceOf
this aggregate type.
To instantiate this AggregateModel, either an AggregateModel
can be provided directly or an
aggregateType
of type Class
can be used. The latter will internally resolve to an
AggregateModel. Thus, either the AggregateModel or the aggregateType
should be provided. An
AxonConfigurationException
is thrown if this criteria is not met.
The same criteria holds for the AggregateFactory
. Either the AggregateFactory can be set directly or it
will be instantiated internally based on the aggregateType
. Hence, one of both is a hard requirement, and
will also result in an AxonConfigurationException if both are missing.
Additionally will assert that the LockFactory
, EventStore
and SnapshotTriggerDefinition
are not null
, resulting in an AxonConfigurationException if for any of these this is the case.
builder
- the EventSourcingRepository.Builder
used to instantiate a EventSourcingRepository
instancepublic static <T> EventSourcingRepository.Builder<T> builder(Class<T> aggregateType)
EventSourcingRepository
for aggregate type T
.
Can also be used to instantiate a CachingEventSourcingRepository
for aggregate type T
. This
Builder will check whether a Cache
is provided. If this holds, the EventSourcingRepository.Builder.build()
function
returns a CachingEventSourcingRepository instead of an EventSourcingRepository.
The LockFactory
is defaulted to an PessimisticLockFactory
and the
SnapshotTriggerDefinition
to a NoSnapshotTriggerDefinition
implementation.
A goal of this Builder goal is to create an AggregateModel
specifying generic T
as the aggregate
type to be stored. All aggregates in this repository must be instanceOf
this aggregate type. To
instantiate this AggregateModel, either an AggregateModel
can be provided directly or an
aggregateType
of type Class
can be used. The latter will internally resolve to an AggregateModel.
Thus, either the AggregateModel or the aggregateType
should be provided.
The same criteria holds for the AggregateFactory
. Either the AggregateFactory can be set directly or it
will be instantiated internally based on the aggregateType
. Hence, one of both is a hard requirement.
Additionally, the EventStore
is a hard requirement and as such should be provided.
EventSourcingRepository
protected EventSourcedAggregate<T> doLoadWithLock(String aggregateIdentifier, Long expectedVersion)
doLoadWithLock
in class LockingRepository<T,EventSourcedAggregate<T>>
aggregateIdentifier
- the identifier of the aggregate to loadexpectedVersion
- The expected version of the loaded aggregateAggregateDeletedException
- in case an aggregate existed in the past, but has been deletedAggregateNotFoundException
- when an aggregate with the given identifier does not existprotected DomainEventStream readEvents(String aggregateIdentifier)
aggregateIdentifier
- the identifier of the aggregate to loadeventStreamFilter
applied if
one was configuredprotected void validateOnLoad(Aggregate<T> aggregate, Long expectedVersion)
AbstractRepository
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
validateOnLoad
in class AbstractRepository<T,LockAwareAggregate<T,EventSourcedAggregate<T>>>
aggregate
- The loaded aggregateexpectedVersion
- The expected version of the aggregateprotected void reportIllegalState(LockAwareAggregate<T,EventSourcedAggregate<T>> aggregate)
AbstractRepository
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 AbstractRepository.doSave(Aggregate)
, AbstractRepository.doDelete(Aggregate)
,
AbstractRepository.postSave(Aggregate)
and AbstractRepository.postDelete(Aggregate)
are not invoked. Implementations may choose to
invoke these methods.
reportIllegalState
in class AbstractRepository<T,LockAwareAggregate<T,EventSourcedAggregate<T>>>
aggregate
- The aggregate instance with illegal stateprotected EventSourcedAggregate<T> doCreateNewForLock(Callable<T> factoryMethod) throws Exception
LockingRepository
factoryMethod
. Implementations should assume that this
method is only called if a UnitOfWork is currently active.doCreateNewForLock
in class LockingRepository<T,EventSourcedAggregate<T>>
factoryMethod
- The method to create the aggregate's root instanceException
- when the factoryMethod throws an exceptionprotected void doSaveWithLock(EventSourcedAggregate<T> aggregate)
LockingRepository
doSaveWithLock
in class LockingRepository<T,EventSourcedAggregate<T>>
aggregate
- the aggregate to storeprotected void doDeleteWithLock(EventSourcedAggregate<T> aggregate)
LockingRepository
doDeleteWithLock
in class LockingRepository<T,EventSourcedAggregate<T>>
aggregate
- the aggregate to deletepublic AggregateFactory<T> getAggregateFactory()
Copyright © 2010–2022. All rights reserved.