Interface AggregateRepository

All Known Implementing Classes:
CachingAggregateRepository, DefaultAggregateRepository

public interface AggregateRepository
Repository interface for loading, managing, and repairing aggregates in Flux Capacitor.

Aggregates in Flux Capacitor can be persisted using different storage strategies, including event sourcing and document-based storage (e.g., DocumentStore). The AggregateRepository abstracts these details and provides a unified API to interact with aggregate instances as Entity wrappers.

Core capabilities include:

  • Loading aggregates by ID, whether event-sourced or document-based.
  • Mapping entities to their associated aggregates (e.g., for child-to-parent traversal).
  • Repairing relationships between entities and aggregates (especially useful after structural refactors).
  • Deleting aggregates and their associated data.

Note: While this interface is accessible directly, most typical usage is through the static FluxCapacitor.loadAggregate(...) or via delegation in Entity classes.

  • Method Details

    • load

      default <T> Entity<T> load(Id<T> aggregateId)
      Load an aggregate by a typed identifier.
      Type Parameters:
      T - the aggregate type.
      Parameters:
      aggregateId - the typed ID of the aggregate.
      Returns:
      the aggregate as a loaded Entity wrapper.
    • load

      default <T> Entity<T> load(@NotNull @NotNull Object aggregateId)
      Load an aggregate by its identifier. If the aggregate exists, it will be loaded and returned with its respective type, if not, an empty Entity of type Object will be returned.
      Type Parameters:
      T - the aggregate type.
      Parameters:
      aggregateId - the aggregate identifier.
      Returns:
      the loaded aggregate wrapped in an Entity.
    • load

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

      <T> Entity<T> loadFor(@NonNull @NonNull Object entityId, Class<?> defaultType)
      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.

      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

      <T> Entity<T> asEntity(T entity)
      Wrap an existing aggregate instance into an Entity, initializing tracking and identity information.
      Type Parameters:
      T - the aggregate type.
      Parameters:
      entity - the aggregate instance.
      Returns:
      the entity wrapper.
    • repairRelationships

      default CompletableFuture<Void> repairRelationships(Id<?> aggregateId)
      Repairs the relationships of the aggregate corresponding to the given typed ID.
      Parameters:
      aggregateId - the typed identifier of the aggregate.
      Returns:
      a future that completes when the repair process is done.
    • repairRelationships

      default CompletableFuture<Void> repairRelationships(Object aggregateId)
      Repairs the relationships of the aggregate with the given ID.
      Parameters:
      aggregateId - the ID of the aggregate.
      Returns:
      a future that completes when the repair process is done.
    • repairRelationships

      CompletableFuture<Void> repairRelationships(Entity<?> aggregate)
      Repairs the internal relationship model for a loaded aggregate.

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

      Parameters:
      aggregate - the aggregate to inspect.
      Returns:
      a future that completes when the relationships are updated.
    • getAggregatesFor

      Map<String,Class<?>> getAggregatesFor(Object entityId)
      Returns a map of aggregate IDs and their types that are associated with a given entity ID.
      Parameters:
      entityId - the child or nested entity.
      Returns:
      a map of aggregate IDs to class names.
    • getLatestAggregateId

      default Optional<String> getLatestAggregateId(Object entityId)
      Returns the most recently created aggregate ID associated with a given entity ID, if any.
      Parameters:
      entityId - the child or nested entity.
      Returns:
      the latest aggregate ID, if present.
    • deleteAggregate

      CompletableFuture<Void> deleteAggregate(Object aggregateId)
      Deletes the persisted state for an aggregate, including its events or document and relationships.
      Parameters:
      aggregateId - the ID of the aggregate to delete.
      Returns:
      a future that completes when deletion has been confirmed.