Interface FlexAggregateRepository<ID,​AGGREGATE_TYPE extends FlexAggregate<ID,​AGGREGATE_TYPE>>

  • Type Parameters:
    ID - the aggregate id type
    AGGREGATE_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:
    FlexAggregate
    • 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 uses
      Class<AGGREGATE_TYPE> aggregateRootImplementationType()
      The type of FlexAggregate implementation this repository handles
      dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateType aggregateType()
      The type of AggregateType this repository is using to persist Events
      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 a FlexAggregateRepository - the EventStore will be configured with the supplied eventStreamConfiguration.
      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 a FlexAggregateRepository - if missing, the EventStore will be configured with the default AggregateEventStreamConfiguration based on the AggregateEventStreamConfigurationFactory that the EventStore's AggregateEventStreamPersistenceStrategy is configured with
      default AGGREGATE_TYPE load​(ID aggregateId)
      Load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
      The loaded aggregate instance will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
      default AGGREGATE_TYPE load​(ID aggregateId, long expectedLatestEventOrder)
      Load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
      The loaded aggregate instance will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
      void persist​(EventsToPersist<ID,​Object> eventsToPersist)
      Associate a newly created and not yet persisted FlexAggregate instance with the UnitOfWorkFactory.getRequiredUnitOfWork()
      Any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
      default Optional<AGGREGATE_TYPE> tryLoad​(ID aggregateId)
      Try to load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
      If the aggregate instance exists it will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
      default Optional<AGGREGATE_TYPE> tryLoad​(ID aggregateId, long expectedLatestEventOrder)
      Try to load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
      If the aggregate instance exists it will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
      Optional<AGGREGATE_TYPE> tryLoad​(ID aggregateId, Optional<Long> expectedLatestEventOrder)
      Try to load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
      If the aggregate instance exists it will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is 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 a FlexAggregateRepository - the EventStore will be configured with the supplied eventStreamConfiguration.
        Type Parameters:
        ID - the aggregate ID type
        AGGREGATE_TYPE - the concrete aggregate type (MUST be a subtype of FlexAggregate)
        Parameters:
        eventStore - The EventStore instance to use
        aggregateEventStreamConfiguration - the configuration for the event stream that will contain all the events related to the aggregate type
        unitOfWorkFactory - The factory that provides UnitOfWork's
        aggregateIdType - the concrete aggregate ID type
        aggregateImplementationType - the concrete aggregate type (MUST be a subtype of FlexAggregate)
        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 a FlexAggregateRepository - if missing, the EventStore will be configured with the default AggregateEventStreamConfiguration based on the AggregateEventStreamConfigurationFactory that the EventStore's AggregateEventStreamPersistenceStrategy is configured with
        Type Parameters:
        ID - the aggregate ID type
        AGGREGATE_TYPE - the concrete aggregate type (MUST be a subtype of FlexAggregate)
        Parameters:
        eventStore - The EventStore instance to use
        aggregateType - the aggregate type being handled by this repository
        unitOfWorkFactory - The factory that provides UnitOfWork's
        aggregateIdType - the concrete aggregate ID type
        aggregateImplementationType - the concrete aggregate type (MUST be a subtype of FlexAggregate)
        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 an FlexAggregate instance with the specified aggregateId from the underlying EventStore
        If the aggregate instance exists it will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
        Parameters:
        aggregateId - the id of the aggregate we want to load
        expectedLatestEventOrder - the expected PersistedEvent.eventOrder() of the last event stored in relation to the given aggregate instance
        Returns:
        an Optional with the matching FlexAggregate instance if it exists, otherwise it will return an Optional.empty()
        Throws:
        OptimisticAggregateLoadException - in case the PersistedEvent.eventOrder() of the last event stored in relation to the given aggregate instance is different from the expectedLatestEventOrder
      • tryLoad

        Optional<AGGREGATE_TYPE> tryLoad​(ID aggregateId,
                                         Optional<Long> expectedLatestEventOrder)
        Try to load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
        If the aggregate instance exists it will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
        Parameters:
        aggregateId - the id of the aggregate we want to load
        expectedLatestEventOrder - Optional with the expected PersistedEvent.eventOrder() of the last event stored in relation to the given aggregate instance (if any)
        Returns:
        an Optional with the matching FlexAggregate instance if it exists, otherwise it will return an Optional.empty()
        Throws:
        OptimisticAggregateLoadException - in case the PersistedEvent.eventOrder() of the last event stored in relation to the given aggregate instance is different from the expectedLatestEventOrder
      • tryLoad

        default Optional<AGGREGATE_TYPE> tryLoad​(ID aggregateId)
        Try to load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
        If the aggregate instance exists it will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
        Parameters:
        aggregateId - the id of the aggregate we want to load
        Returns:
        an Optional with the matching FlexAggregate instance if it exists, otherwise it will return an Optional.empty()
        Throws:
        OptimisticAggregateLoadException - in case the PersistedEvent.eventOrder() of the last event stored in relation to the given aggregate instance is different from the expectedLatestEventOrder
      • load

        default AGGREGATE_TYPE load​(ID aggregateId)
        Load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
        The loaded aggregate instance will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
        Parameters:
        aggregateId - the id of the aggregate we want to load
        Returns:
        an Optional with the matching FlexAggregate instance
        Throws:
        dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.AggregateNotFoundException - in case a matching FlexAggregate doesn't exist in the EventStore
      • aggregateType

        dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateType aggregateType()
        The type of AggregateType this repository is using to persist Events
      • load

        default AGGREGATE_TYPE load​(ID aggregateId,
                                    long expectedLatestEventOrder)
        Load an FlexAggregate instance with the specified aggregateId from the underlying EventStore
        The loaded aggregate instance will be associated with the UnitOfWorkFactory.getRequiredUnitOfWork() and any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
        Parameters:
        aggregateId - the id of the aggregate we want to load
        expectedLatestEventOrder - the expected PersistedEvent.eventOrder() of the last event stored in relation to the given aggregate instance
        Returns:
        an Optional with the matching FlexAggregate instance
        Throws:
        dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.AggregateNotFoundException - in case a matching FlexAggregate doesn't exist in the EventStore
      • persist

        void persist​(EventsToPersist<ID,​Object> eventsToPersist)
        Associate a newly created and not yet persisted FlexAggregate instance with the UnitOfWorkFactory.getRequiredUnitOfWork()
        Any changes to will be tracked and will be persisted to the underlying EventStore when the UnitOfWork is committed.
        Parameters:
        eventsToPersist - the events to persist to the underlying EventStore (a result of a Command method invocation on an FlexAggregate instance
      • aggregateIdType

        Class<ID> aggregateIdType()
        The type of aggregate ID this repository uses