Interface FlexAggregateRepository<ID,AGGREGATE_TYPE extends FlexAggregate<ID,AGGREGATE_TYPE>>
-
- Type Parameters:
ID- the aggregate id typeAGGREGATE_TYPE- the aggregate type
- All Known Implementing Classes:
FlexAggregateRepository.DefaultFlexAggregateRepository
public interface FlexAggregateRepository<ID,AGGREGATE_TYPE extends FlexAggregate<ID,AGGREGATE_TYPE>>OpinionatedFlexAggregateRepository that's built to persist and load a specificFlexAggregatetype in combination withEventStore,EventStoreUnitOfWorkFactoryand aFlexAggregateRepository.
Here's how to create an
FlexAggregateRepositoryinstance that can persist anFlexAggregateof typeOrderwhich has an aggregate id of typeOrderId:
Here's a typical usage pattern for when you want to persist an newFlexAggregateRepository<OrderId, Order> repository = FlexAggregateRepository.from( eventStores, standardSingleTenantConfigurationUsingJackson( AggregateType.of("Orders"), createObjectMapper(), AggregateIdSerializer.serializerFor(OrderId.class), IdentifierColumnType.UUID, JSONColumnType.JSONB), unitOfWorkFactory, OrderId.class, Order.class );FlexAggregateinstance (i.e. theEventStoredoesn't contain an events related to the given Aggregate id):
Here's the typical usage pattern forunitOfWorkFactory.usingUnitOfWork(unitOfWork -> { var eventsToPersist = Order.createNewOrder(orderId, CustomerId.random(), 123); repository.persist(eventsToPersist); });FlexAggregateRepositoryfor already existingFlexAggregateinstance (i.e. an instance that has events in theEventStore):unitOfWorkFactory.usingUnitOfWork(unitOfWork -> { var order = repository.load(orderId); var eventsToPersist = order.accept(); repository.persist(eventsToPersist); });- See Also:
FlexAggregate
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classFlexAggregateRepository.DefaultFlexAggregateRepository<ID,AGGREGATE_TYPE extends FlexAggregate<ID,AGGREGATE_TYPE>>
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Class<ID>aggregateIdType()The type of aggregate ID this repository usesClass<AGGREGATE_TYPE>aggregateRootImplementationType()The type ofFlexAggregateimplementation this repository handlesdk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateTypeaggregateType()The type ofAggregateTypethis repository is using to persist Eventsstatic <CONFIG extends dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.persistence.AggregateEventStreamConfiguration,ID,AGGREGATE_TYPE extends FlexAggregate<ID,AGGREGATE_TYPE>>
FlexAggregateRepository<ID,AGGREGATE_TYPE>from(dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.ConfigurableEventStore<CONFIG> eventStore, CONFIG aggregateEventStreamConfiguration, dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.transaction.EventStoreUnitOfWorkFactory unitOfWorkFactory, Class<ID> aggregateIdType, Class<AGGREGATE_TYPE> aggregateImplementationType)Create aFlexAggregateRepository- theEventStorewill be configured with the suppliedeventStreamConfiguration.static <CONFIG extends dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.persistence.AggregateEventStreamConfiguration,ID,AGGREGATE_TYPE extends FlexAggregate<ID,AGGREGATE_TYPE>>
FlexAggregateRepository<ID,AGGREGATE_TYPE>from(dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.ConfigurableEventStore<CONFIG> eventStore, dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateType aggregateType, dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.transaction.EventStoreUnitOfWorkFactory unitOfWorkFactory, Class<ID> aggregateIdType, Class<AGGREGATE_TYPE> aggregateImplementationType)Create aFlexAggregateRepository- if missing, theEventStorewill be configured with the defaultAggregateEventStreamConfigurationbased on theAggregateEventStreamConfigurationFactorythat theEventStore'sAggregateEventStreamPersistenceStrategyis configured withdefault AGGREGATE_TYPEload(ID aggregateId)Load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
The loaded aggregate instance will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.default AGGREGATE_TYPEload(ID aggregateId, long expectedLatestEventOrder)Load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
The loaded aggregate instance will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.voidpersist(EventsToPersist<ID,Object> eventsToPersist)Associate a newly created and not yet persistedFlexAggregateinstance with theUnitOfWorkFactory.getRequiredUnitOfWork()
Any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.default Optional<AGGREGATE_TYPE>tryLoad(ID aggregateId)Try to load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
If the aggregate instance exists it will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.default Optional<AGGREGATE_TYPE>tryLoad(ID aggregateId, long expectedLatestEventOrder)Try to load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
If the aggregate instance exists it will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.Optional<AGGREGATE_TYPE>tryLoad(ID aggregateId, Optional<Long> expectedLatestEventOrder)Try to load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
If the aggregate instance exists it will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.
-
-
-
Method Detail
-
from
static <CONFIG extends dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.persistence.AggregateEventStreamConfiguration,ID,AGGREGATE_TYPE extends FlexAggregate<ID,AGGREGATE_TYPE>> FlexAggregateRepository<ID,AGGREGATE_TYPE> from(dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.ConfigurableEventStore<CONFIG> eventStore, CONFIG aggregateEventStreamConfiguration, dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.transaction.EventStoreUnitOfWorkFactory unitOfWorkFactory, Class<ID> aggregateIdType, Class<AGGREGATE_TYPE> aggregateImplementationType)
Create aFlexAggregateRepository- theEventStorewill be configured with the suppliedeventStreamConfiguration.- Type Parameters:
ID- the aggregate ID typeAGGREGATE_TYPE- the concrete aggregate type (MUST be a subtype ofFlexAggregate)- Parameters:
eventStore- TheEventStoreinstance to useaggregateEventStreamConfiguration- the configuration for the event stream that will contain all the events related to the aggregate typeunitOfWorkFactory- The factory that providesUnitOfWork'saggregateIdType- the concrete aggregate ID typeaggregateImplementationType- the concrete aggregate type (MUST be a subtype ofFlexAggregate)- Returns:
- a repository instance that can be used load, add and query aggregates of type
aggregateImplementationType
-
from
static <CONFIG extends dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.persistence.AggregateEventStreamConfiguration,ID,AGGREGATE_TYPE extends FlexAggregate<ID,AGGREGATE_TYPE>> FlexAggregateRepository<ID,AGGREGATE_TYPE> from(dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.ConfigurableEventStore<CONFIG> eventStore, dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateType aggregateType, dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.transaction.EventStoreUnitOfWorkFactory unitOfWorkFactory, Class<ID> aggregateIdType, Class<AGGREGATE_TYPE> aggregateImplementationType)
Create aFlexAggregateRepository- if missing, theEventStorewill be configured with the defaultAggregateEventStreamConfigurationbased on theAggregateEventStreamConfigurationFactorythat theEventStore'sAggregateEventStreamPersistenceStrategyis configured with- Type Parameters:
ID- the aggregate ID typeAGGREGATE_TYPE- the concrete aggregate type (MUST be a subtype ofFlexAggregate)- Parameters:
eventStore- TheEventStoreinstance to useaggregateType- the aggregate type being handled by this repositoryunitOfWorkFactory- The factory that providesUnitOfWork'saggregateIdType- the concrete aggregate ID typeaggregateImplementationType- the concrete aggregate type (MUST be a subtype ofFlexAggregate)- Returns:
- a repository instance that can be used load, add and query aggregates of type
aggregateImplementationType
-
tryLoad
default Optional<AGGREGATE_TYPE> tryLoad(ID aggregateId, long expectedLatestEventOrder)
Try to load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
If the aggregate instance exists it will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.- Parameters:
aggregateId- the id of the aggregate we want to loadexpectedLatestEventOrder- the expectedPersistedEvent.eventOrder()of the last event stored in relation to the given aggregate instance- Returns:
- an
Optionalwith the matchingFlexAggregateinstance if it exists, otherwise it will return anOptional.empty() - Throws:
OptimisticAggregateLoadException- in case thePersistedEvent.eventOrder()of the last event stored in relation to the given aggregate instance is different from theexpectedLatestEventOrder
-
tryLoad
Optional<AGGREGATE_TYPE> tryLoad(ID aggregateId, Optional<Long> expectedLatestEventOrder)
Try to load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
If the aggregate instance exists it will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.- Parameters:
aggregateId- the id of the aggregate we want to loadexpectedLatestEventOrder- Optional with the expectedPersistedEvent.eventOrder()of the last event stored in relation to the given aggregate instance (if any)- Returns:
- an
Optionalwith the matchingFlexAggregateinstance if it exists, otherwise it will return anOptional.empty() - Throws:
OptimisticAggregateLoadException- in case thePersistedEvent.eventOrder()of the last event stored in relation to the given aggregate instance is different from theexpectedLatestEventOrder
-
tryLoad
default Optional<AGGREGATE_TYPE> tryLoad(ID aggregateId)
Try to load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
If the aggregate instance exists it will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.- Parameters:
aggregateId- the id of the aggregate we want to load- Returns:
- an
Optionalwith the matchingFlexAggregateinstance if it exists, otherwise it will return anOptional.empty() - Throws:
OptimisticAggregateLoadException- in case thePersistedEvent.eventOrder()of the last event stored in relation to the given aggregate instance is different from theexpectedLatestEventOrder
-
load
default AGGREGATE_TYPE load(ID aggregateId)
Load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
The loaded aggregate instance will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.- Parameters:
aggregateId- the id of the aggregate we want to load- Returns:
- an
Optionalwith the matchingFlexAggregateinstance - Throws:
dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.AggregateNotFoundException- in case a matchingFlexAggregatedoesn't exist in theEventStore
-
aggregateRootImplementationType
Class<AGGREGATE_TYPE> aggregateRootImplementationType()
The type ofFlexAggregateimplementation this repository handles
-
aggregateType
dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateType aggregateType()
The type ofAggregateTypethis repository is using to persist Events
-
load
default AGGREGATE_TYPE load(ID aggregateId, long expectedLatestEventOrder)
Load anFlexAggregateinstance with the specifiedaggregateIdfrom the underlyingEventStore
The loaded aggregate instance will be associated with theUnitOfWorkFactory.getRequiredUnitOfWork()and any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.- Parameters:
aggregateId- the id of the aggregate we want to loadexpectedLatestEventOrder- the expectedPersistedEvent.eventOrder()of the last event stored in relation to the given aggregate instance- Returns:
- an
Optionalwith the matchingFlexAggregateinstance - Throws:
dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.AggregateNotFoundException- in case a matchingFlexAggregatedoesn't exist in theEventStore
-
persist
void persist(EventsToPersist<ID,Object> eventsToPersist)
Associate a newly created and not yet persistedFlexAggregateinstance with theUnitOfWorkFactory.getRequiredUnitOfWork()
Any changes to will be tracked and will be persisted to the underlyingEventStorewhen theUnitOfWorkis committed.- Parameters:
eventsToPersist- the events to persist to the underlyingEventStore(a result of a Command method invocation on anFlexAggregateinstance
-
-