Class DefaultAggregateRepository

java.lang.Object
io.fluxzero.sdk.common.AbstractNamespaced<AggregateRepository>
io.fluxzero.sdk.persisting.repository.DefaultAggregateRepository
All Implemented Interfaces:
Namespaced<AggregateRepository>, AggregateRepository

public class DefaultAggregateRepository extends AbstractNamespaced<AggregateRepository> implements AggregateRepository
Default implementation of the AggregateRepository interface.

This class supports aggregates that are either event-sourced or document-based. Its behavior is driven by the @Aggregate annotation on the aggregate class, which determines aspects such as:

  • Whether the aggregate is event-sourced or backed by a document store.
  • If snapshots should be used and their frequency (via snapshotPeriod).
  • Whether the aggregate is searchable (indexed for queries).
  • Whether events should be published and how (via EventPublicationStrategy).

It supports caching of aggregates and entity-aggregate relationship metadata via the provided Cache instances. These are consulted and updated during load and commit operations.

This repository coordinates with several components to manage aggregates:

This implementation also tracks relationships between aggregates and nested entities (e.g., value objects or sub-entities) and can repair these links after structural refactors using repairRelationships(Entity).

The inner AnnotatedAggregateRepository<T> class handles aggregate-specific operations by introspecting the annotations and metadata declared on a given aggregate type.

See Also:
  • Field Details

  • Constructor Details

    • DefaultAggregateRepository

      public DefaultAggregateRepository(Client client, EventStore eventStore, SnapshotStore snapshotStore, io.fluxzero.common.caching.Cache aggregateCache, io.fluxzero.common.caching.Cache relationshipsCache, DocumentStore documentStore, Serializer serializer, DispatchInterceptor dispatchInterceptor, EntityHelper entityHelper)
      Creates a repository whose storage clients and caches can be switched together to another namespace.
      Parameters:
      client - the client used to obtain namespace-specific low-level event store clients
      eventStore - the event store for the client's current namespace
      snapshotStore - the snapshot store for the client's current namespace
      aggregateCache - the shared cache used for aggregates
      relationshipsCache - the shared cache used for aggregate relationships
      documentStore - the document store for the client's current namespace
      serializer - the serializer used for aggregate events
      dispatchInterceptor - interceptor for published aggregate events
      entityHelper - helper for aggregate entity models
  • Method Details

    • createForNamespace

      protected AggregateRepository createForNamespace(String namespace)
      Specified by:
      createForNamespace in class AbstractNamespaced<AggregateRepository>
    • load

      public <T> Entity<T> load(@NonNull @NonNull Object aggregateId, Class<T> type)
      Description copied from interface: AggregateRepository
      Load an aggregate by its identifier and type.
      Specified by:
      load in interface AggregateRepository
      Type Parameters:
      T - the aggregate type.
      Parameters:
      aggregateId - the aggregate identifier.
      type - the expected class type.
      Returns:
      the loaded aggregate wrapped in an Entity.
    • loadFor

      public <T> Entity<T> loadFor(@NonNull @NonNull Object entityId, Class<?> defaultType)
      Description copied from interface: AggregateRepository
      Load the aggregate that owns the specified entity.

      If no ownership is found in the relationship index, this method may fall back to loading the entity as if it were an aggregate itself.

      Specified by:
      loadFor in interface AggregateRepository
      Type Parameters:
      T - the aggregate type.
      Parameters:
      entityId - the child or nested entity.
      defaultType - fallback type to use when no aggregate mapping is available.
      Returns:
      the loaded aggregate as an Entity.
    • asEntity

      public <T> Entity<T> asEntity(T entityValue)
      Description copied from interface: AggregateRepository
      Wrap an existing aggregate instance into an Entity, initializing tracking and identity information.
      Specified by:
      asEntity in interface AggregateRepository
      Type Parameters:
      T - the aggregate type.
      Parameters:
      entityValue - the aggregate instance.
      Returns:
      the entity wrapper.
    • getAggregatesFor

      public Map<String,Class<?>> getAggregatesFor(@NonNull @NonNull Object entityId)
      Description copied from interface: AggregateRepository
      Returns a map of aggregate IDs and their types that are associated with a given entity ID.
      Specified by:
      getAggregatesFor in interface AggregateRepository
      Parameters:
      entityId - the child or nested entity.
      Returns:
      a map of aggregate IDs to class names.
    • deleteAggregate

      public CompletableFuture<Void> deleteAggregate(Object aggregateId)
      Description copied from interface: AggregateRepository
      Deletes the persisted state for an aggregate, including its events or document and relationships.
      Specified by:
      deleteAggregate in interface AggregateRepository
      Parameters:
      aggregateId - the ID of the aggregate to delete.
      Returns:
      a future that completes when deletion has been confirmed.
    • repairRelationships

      public CompletableFuture<Void> repairRelationships(Entity<?> aggregate)
      Description copied from interface: AggregateRepository
      Repairs the internal relationship model for a loaded aggregate.

      This is useful when refactoring entity hierarchies or recovering from inconsistent relationship state.

      Specified by:
      repairRelationships in interface AggregateRepository
      Parameters:
      aggregate - the aggregate to inspect.
      Returns:
      a future that completes when the relationships are updated.