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>>
Opinionated
FlexAggregate Repository that's built to persist and load a specific FlexAggregate type in combination
with EventStore, EventStoreUnitOfWorkFactory and a FlexAggregateRepository.
Here's how to create an FlexAggregateRepository instance that can persist an FlexAggregate
of type Order which has an aggregate id of type OrderId:
FlexAggregateRepository<OrderId, Order> repository =
FlexAggregateRepository.from(
eventStores,
standardSingleTenantConfigurationUsingJackson(
AggregateType.of("Orders"),
createObjectMapper(),
AggregateIdSerializer.serializerFor(OrderId.class),
IdentifierColumnType.UUID,
JSONColumnType.JSONB),
unitOfWorkFactory,
OrderId.class,
Order.class
);
Here's a typical usage pattern for when you want to persist an new FlexAggregate instance
(i.e. the EventStore doesn't contain an events related to the given Aggregate id):
unitOfWorkFactory.usingUnitOfWork(unitOfWork -> {
var eventsToPersist = Order.createNewOrder(orderId, CustomerId.random(), 123);
repository.persist(eventsToPersist);
});
Here's the typical usage pattern for FlexAggregateRepository for already existing FlexAggregate
instance (i.e. an instance that has events in the EventStore):
unitOfWorkFactory.usingUnitOfWork(unitOfWork -> {
var order = repository.load(orderId);
var eventsToPersist = order.accept();
repository.persist(eventsToPersist);
});
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classFlexAggregateRepository.DefaultFlexAggregateRepository<ID,AGGREGATE_TYPE extends FlexAggregate<ID, AGGREGATE_TYPE>> -
Method Summary
Modifier and TypeMethodDescriptionThe type of aggregate ID this repository usesThe type ofFlexAggregateimplementation this repository handlesdk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateTypeThe 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 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 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>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>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.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 Details
-
from
static <CONFIG extends dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.persistence.AggregateEventStreamConfiguration,ID, FlexAggregateRepository<ID,AGGREGATE_TYPE extends FlexAggregate<ID, AGGREGATE_TYPE>> 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, FlexAggregateRepository<ID,AGGREGATE_TYPE extends FlexAggregate<ID, AGGREGATE_TYPE>> 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
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
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
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
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
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
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
-
aggregateIdType
The type of aggregate ID this repository uses
-