Class EventSourcingRepository<T>
- Type Parameters:
T- The type of aggregate this repository stores
- All Implemented Interfaces:
ScopeAware,Repository<T>
- Direct Known Subclasses:
CachingEventSourcingRepository
EventBus and delegate event
storage to the provided EventStore.- Since:
- 0.1
- Author:
- Allard Buijze
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class to instantiate aEventSourcingRepository. -
Field Summary
Fields inherited from class org.axonframework.modelling.command.AbstractRepository
spanFactory -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate aEventSourcingRepositorybased on the fields contained in theEventSourcingRepository.Builder. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> EventSourcingRepository.Builder<T> Instantiate a Builder to be able to create aEventSourcingRepositoryfor aggregate typeT.protected EventSourcedAggregate<T> doCreateNewForLock(Callable<T> factoryMethod) Creates a new aggregate instance using the givenfactoryMethod.protected voiddoDeleteWithLock(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 voiddoSaveWithLock(EventSourcedAggregate<T> aggregate) Perform the actual saving of the aggregate.Returns the factory used by this repository.protected DomainEventStreamreadEvents(String aggregateIdentifier) Reads the events for the given aggregateIdentifier from the eventStore. this method may be overridden to add pre or postprocessing to the loading of an event streamprotected voidreportIllegalState(LockAwareAggregate<T, EventSourcedAggregate<T>> aggregate) Invoked when an the givenaggregateinstance has been detected that has been part of a rolled back Unit of Work.protected voidvalidateOnLoad(Aggregate<T> aggregate, Long expectedVersion) Checks the aggregate for concurrent changes.Methods inherited from class org.axonframework.modelling.command.LockingRepository
doCreateNew, doDelete, doLoad, doLoadOrCreate, doSave, prepareForCommitMethods inherited from class org.axonframework.modelling.command.AbstractRepository
aggregateModel, canResolve, getAggregateType, load, load, loadOrCreate, managedAggregates, newInstance, newInstance, postDelete, postSave, send
-
Constructor Details
-
EventSourcingRepository
Instantiate aEventSourcingRepositorybased on the fields contained in theEventSourcingRepository.Builder.A goal of the provided Builder is to create an
AggregateModelspecifying genericTas the aggregate type to be stored. All aggregates in this repository must beinstanceOfthis aggregate type. To instantiate this AggregateModel, either anAggregateModelcan be provided directly or anaggregateTypeof typeClasscan be used. The latter will internally resolve to an AggregateModel. Thus, either the AggregateModel or theaggregateTypeshould be provided. AnAxonConfigurationExceptionis thrown if these criteria are not met. The same criteria holds for theAggregateFactory. Either the AggregateFactory can be set directly or it will be instantiated internally based on theaggregateType. Hence, one of both is a hard requirement, and will also result in an AxonConfigurationException if both are missing.Additionally, the builder will assert that the
LockFactory,EventStoreandSnapshotTriggerDefinitionare notnull, resulting in an AxonConfigurationException if for any of these this is the case. TheRepositorySpanFactoryis defaulted to aDefaultRepositorySpanFactorybacked by aNoOpSpanFactory.- Parameters:
builder- theEventSourcingRepository.Builderused to instantiate aEventSourcingRepositoryinstance
-
-
Method Details
-
builder
Instantiate a Builder to be able to create aEventSourcingRepositoryfor aggregate typeT. Can also be used to instantiate aCachingEventSourcingRepositoryfor aggregate typeT. This Builder will check whether aCacheis provided. If this holds, theEventSourcingRepository.Builder.build()function returns a CachingEventSourcingRepository instead of an EventSourcingRepository.The
LockFactoryis defaulted to anPessimisticLockFactoryand theSnapshotTriggerDefinitionto aNoSnapshotTriggerDefinitionimplementation. A goal of this Builder goal is to create anAggregateModelspecifying genericTas the aggregate type to be stored. All aggregates in this repository must beinstanceOfthis aggregate type. To instantiate this AggregateModel, either anAggregateModelcan be provided directly or anaggregateTypeof typeClasscan be used. The latter will internally resolve to an AggregateModel. Thus, either the AggregateModel or theaggregateTypeshould be provided. The same criteria holds for theAggregateFactory. Either the AggregateFactory can be set directly or it will be instantiated internally based on theaggregateType. Hence, one of both is a hard requirement.Additionally, the
EventStoreis a hard requirement and as such should be provided.- Returns:
- a Builder to be able to create a
EventSourcingRepository
-
doLoadWithLock
Perform the actual loading of an aggregate. The necessary locks have been obtained.- Specified by:
doLoadWithLockin classLockingRepository<T,EventSourcedAggregate<T>> - Parameters:
aggregateIdentifier- the identifier of the aggregate to loadexpectedVersion- The expected version of the loaded aggregate- Returns:
- the fully initialized aggregate
- Throws:
AggregateDeletedException- in case an aggregate existed in the past, but has been deletedAggregateNotFoundException- when an aggregate with the given identifier does not exist
-
readEvents
Reads the events for the given aggregateIdentifier from the eventStore. this method may be overridden to add pre or postprocessing to the loading of an event stream- Parameters:
aggregateIdentifier- the identifier of the aggregate to load- Returns:
- the domain event stream for the given aggregateIdentifier, with
eventStreamFilterapplied if one was configured
-
validateOnLoad
Description copied from class:AbstractRepositoryChecks the aggregate for concurrent changes. Throws aConflictingModificationExceptionwhen conflicting changes have been detected.This implementation throws a
ConflictingAggregateVersionExceptionif the expected version is not null and the version number of the aggregate does not match the expected version- Overrides:
validateOnLoadin classAbstractRepository<T,LockAwareAggregate<T, EventSourcedAggregate<T>>> - Parameters:
aggregate- The loaded aggregateexpectedVersion- The expected version of the aggregate
-
reportIllegalState
Description copied from class:AbstractRepositoryInvoked when an the givenaggregateinstance 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)andAbstractRepository.postDelete(Aggregate)are not invoked. Implementations may choose to invoke these methods.- Overrides:
reportIllegalStatein classAbstractRepository<T,LockAwareAggregate<T, EventSourcedAggregate<T>>> - Parameters:
aggregate- The aggregate instance with illegal state
-
doCreateNewForLock
Description copied from class:LockingRepositoryCreates a new aggregate instance using the givenfactoryMethod. Implementations should assume that this method is only called if a UnitOfWork is currently active.- Specified by:
doCreateNewForLockin classLockingRepository<T,EventSourcedAggregate<T>> - Parameters:
factoryMethod- The method to create the aggregate's root instance- Returns:
- an Aggregate instance describing the aggregate's state
- Throws:
Exception- when the factoryMethod throws an exception
-
doSaveWithLock
Description copied from class:LockingRepositoryPerform the actual saving of the aggregate. All necessary locks have been verified.- Specified by:
doSaveWithLockin classLockingRepository<T,EventSourcedAggregate<T>> - Parameters:
aggregate- the aggregate to store
-
doDeleteWithLock
Description copied from class:LockingRepositoryPerform the actual deleting of the aggregate. All necessary locks have been verified.- Specified by:
doDeleteWithLockin classLockingRepository<T,EventSourcedAggregate<T>> - Parameters:
aggregate- the aggregate to delete
-
getAggregateFactory
Returns the factory used by this repository.- Returns:
- the factory used by this repository
-