Class DefaultAggregateRepository
- All Implemented Interfaces:
AggregateRepository
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:
EventStoreandEventStoreClientfor storing and loading events.SnapshotStorefor loading and storing snapshots.DocumentStorefor searchable aggregates (non-event-sourced).DispatchInterceptorto inspect or modify outgoing events before dispatch.
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassAggregate-type-specific delegate used internally byDefaultAggregateRepository. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultAggregateRepository(EventStore eventStore, EventStoreClient eventStoreClient, SnapshotStore snapshotStore, io.fluxzero.common.caching.Cache aggregateCache, io.fluxzero.common.caching.Cache relationshipsCache, DocumentStore documentStore, Serializer serializer, DispatchInterceptor dispatchInterceptor, EntityHelper entityHelper) -
Method Summary
Modifier and TypeMethodDescription<T> Entity<T> asEntity(T entityValue) Wrap an existing aggregate instance into anEntity, initializing tracking and identity information.deleteAggregate(Object aggregateId) Deletes the persisted state for an aggregate, including its events or document and relationships.getAggregatesFor(@NonNull Object entityId) Returns a map of aggregate IDs and their types that are associated with a given entity ID.<T> Entity<T> Load an aggregate by its identifier and type.<T> Entity<T> Load the aggregate that owns the specified entity.repairRelationships(Entity<?> aggregate) Repairs the internal relationship model for a loaded aggregate.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface AggregateRepository
getLatestAggregateId, load, load, repairRelationships, repairRelationships
-
Constructor Details
-
DefaultAggregateRepository
public DefaultAggregateRepository(EventStore eventStore, EventStoreClient eventStoreClient, SnapshotStore snapshotStore, io.fluxzero.common.caching.Cache aggregateCache, io.fluxzero.common.caching.Cache relationshipsCache, DocumentStore documentStore, Serializer serializer, DispatchInterceptor dispatchInterceptor, EntityHelper entityHelper)
-
-
Method Details
-
load
Description copied from interface:AggregateRepositoryLoad an aggregate by its identifier and type.- Specified by:
loadin interfaceAggregateRepository- 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
Description copied from interface:AggregateRepositoryLoad 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:
loadForin interfaceAggregateRepository- 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
Description copied from interface:AggregateRepositoryWrap an existing aggregate instance into anEntity, initializing tracking and identity information.- Specified by:
asEntityin interfaceAggregateRepository- Type Parameters:
T- the aggregate type.- Parameters:
entityValue- the aggregate instance.- Returns:
- the entity wrapper.
-
getAggregatesFor
Description copied from interface:AggregateRepositoryReturns a map of aggregate IDs and their types that are associated with a given entity ID.- Specified by:
getAggregatesForin interfaceAggregateRepository- Parameters:
entityId- the child or nested entity.- Returns:
- a map of aggregate IDs to class names.
-
deleteAggregate
Description copied from interface:AggregateRepositoryDeletes the persisted state for an aggregate, including its events or document and relationships.- Specified by:
deleteAggregatein interfaceAggregateRepository- Parameters:
aggregateId- the ID of the aggregate to delete.- Returns:
- a future that completes when deletion has been confirmed.
-
repairRelationships
Description copied from interface:AggregateRepositoryRepairs the internal relationship model for a loaded aggregate.This is useful when refactoring entity hierarchies or recovering from inconsistent relationship state.
- Specified by:
repairRelationshipsin interfaceAggregateRepository- Parameters:
aggregate- the aggregate to inspect.- Returns:
- a future that completes when the relationships are updated.
-