Class DefaultAggregateRepository
- All Implemented Interfaces:
Namespaced<AggregateRepository>, 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. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringProperty that overrides the commit policy for aggregates whose annotation usesAggregateCommitPolicy.DEFAULT. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultAggregateRepository(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. -
Method Summary
Modifier and TypeMethodDescription<T> Entity<T> asEntity(T entityValue) Wrap an existing aggregate instance into anEntity, initializing tracking and identity information.protected AggregateRepositorycreateForNamespace(String namespace) 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 AbstractNamespaced
close, forNamespaceMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface AggregateRepository
forNamespace, getLatestAggregateId, load, load, repairRelationships, repairRelationshipsMethods inherited from interface Namespaced
forApplicationNamespace, forDefaultNamespace
-
Field Details
-
AGGREGATE_COMMIT_POLICY_PROPERTY
Property that overrides the commit policy for aggregates whose annotation usesAggregateCommitPolicy.DEFAULT.- See Also:
-
-
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 clientseventStore- the event store for the client's current namespacesnapshotStore- the snapshot store for the client's current namespaceaggregateCache- the shared cache used for aggregatesrelationshipsCache- the shared cache used for aggregate relationshipsdocumentStore- the document store for the client's current namespaceserializer- the serializer used for aggregate eventsdispatchInterceptor- interceptor for published aggregate eventsentityHelper- helper for aggregate entity models
-
-
Method Details
-
createForNamespace
- Specified by:
createForNamespacein classAbstractNamespaced<AggregateRepository>
-
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.
-