org.axonframework.eventsourcing
Class EventSourcingRepository<T extends EventSourcedAggregateRoot>

java.lang.Object
  extended by org.axonframework.repository.AbstractRepository<T>
      extended by org.axonframework.repository.LockingRepository<T>
          extended by org.axonframework.eventsourcing.EventSourcingRepository<T>
Type Parameters:
T - The type of aggregate this repository stores
All Implemented Interfaces:
Repository<T>
Direct Known Subclasses:
CachingEventSourcingRepository

public class EventSourcingRepository<T extends EventSourcedAggregateRoot>
extends LockingRepository<T>

Abstract repository implementation that allows easy implementation of an Event Sourcing mechanism. It will automatically publish new events to the given EventBus and delegate event storage to the provided EventStore.

Since:
0.1
Author:
Allard Buijze
See Also:
EventSourcedAggregateRoot, AbstractEventSourcedAggregateRoot, AbstractAnnotatedAggregateRoot, EventStore

Constructor Summary
EventSourcingRepository(AggregateFactory<T> aggregateFactory, EventStore eventStore)
          Initializes a repository with the default locking strategy, using the given aggregateFactory to create new aggregate instances.
EventSourcingRepository(AggregateFactory<T> aggregateFactory, EventStore eventStore, LockManager lockManager)
          Initialize a repository with the given locking strategy.
EventSourcingRepository(Class<T> aggregateType, EventStore eventStore)
          Initializes a repository with the default locking strategy, using a GenericAggregateFactory to create new aggregate instances of given aggregateType.
EventSourcingRepository(Class<T> aggregateType, EventStore eventStore, LockManager lockManager)
          Initialize a repository with the given locking strategy, using a GenericAggregateFactory to create new aggregate instances.
 
Method Summary
protected  void doDeleteWithLock(T aggregate)
          Delegates to doSaveWithLock(EventSourcedAggregateRoot), as Event Sourcing generally doesn't delete aggregates (not their events).
protected  T doLoad(Object aggregateIdentifier, Long expectedVersion)
          Perform the actual loading of an aggregate.
protected  void doSaveWithLock(T aggregate)
          Perform the actual saving of the aggregate.
 AggregateFactory<T> getAggregateFactory()
          Returns the factory used by this repository.
 String getTypeIdentifier()
          Return the type identifier belonging to the AggregateFactory of this repository.
protected  void resolveConflicts(T aggregate, DomainEventStream unseenEvents)
          Resolve (potential) conflicts for the given aggregate, where given unseenEvents may have been concurrently applied.
 void setConflictResolver(ConflictResolver conflictResolver)
          Sets the conflict resolver to use for this repository.
 void setEventStreamDecorators(List<? extends EventStreamDecorator> eventProcessors)
          Sets the Event Stream Decorators that will process the event in the DomainEventStream when read, or written to the event store.
 void setSnapshotterTrigger(SnapshotterTrigger snapshotterTrigger)
          Sets the snapshotter trigger for this repository.
protected  void validateOnLoad(T aggregate, Long expectedVersion)
          Checks the aggregate for concurrent changes.
 
Methods inherited from class org.axonframework.repository.LockingRepository
add, doDelete, doSave, load
 
Methods inherited from class org.axonframework.repository.AbstractRepository
getAggregateType, load, postDelete, postSave, setEventBus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EventSourcingRepository

public EventSourcingRepository(Class<T> aggregateType,
                               EventStore eventStore)
Initializes a repository with the default locking strategy, using a GenericAggregateFactory to create new aggregate instances of given aggregateType.

Parameters:
aggregateType - The type of aggregate stored in this repository
eventStore - The event store that holds the event streams for this repository
See Also:
LockingRepository.LockingRepository(Class)

EventSourcingRepository

public EventSourcingRepository(AggregateFactory<T> aggregateFactory,
                               EventStore eventStore)
Initializes a repository with the default locking strategy, using the given aggregateFactory to create new aggregate instances.

Parameters:
aggregateFactory - The factory for new aggregate instances
eventStore - The event store that holds the event streams for this repository
See Also:
LockingRepository.LockingRepository(Class)

EventSourcingRepository

public EventSourcingRepository(AggregateFactory<T> aggregateFactory,
                               EventStore eventStore,
                               LockManager lockManager)
Initialize a repository with the given locking strategy.

Parameters:
aggregateFactory - The factory for new aggregate instances
eventStore - The event store that holds the event streams for this repository
lockManager - the locking strategy to apply to this repository

EventSourcingRepository

public EventSourcingRepository(Class<T> aggregateType,
                               EventStore eventStore,
                               LockManager lockManager)
Initialize a repository with the given locking strategy, using a GenericAggregateFactory to create new aggregate instances.

Parameters:
aggregateType - The type of aggregate to store in this repository
eventStore - The event store that holds the event streams for this repository
lockManager - the locking strategy to apply to this
Method Detail

doSaveWithLock

protected void doSaveWithLock(T aggregate)
Perform the actual saving of the aggregate. All necessary locks have been verified.

Specified by:
doSaveWithLock in class LockingRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregate - the aggregate to store

doDeleteWithLock

protected void doDeleteWithLock(T aggregate)
Delegates to doSaveWithLock(EventSourcedAggregateRoot), as Event Sourcing generally doesn't delete aggregates (not their events).

This method may be safely overridden for special cases that do require deleting an Aggregate's Events.

Specified by:
doDeleteWithLock in class LockingRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregate - the aggregate to delete

doLoad

protected T doLoad(Object aggregateIdentifier,
                   Long expectedVersion)
Perform the actual loading of an aggregate. The necessary locks have been obtained.

Specified by:
doLoad in class LockingRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregateIdentifier - the identifier of the aggregate to load
expectedVersion - The expected version of the loaded aggregate
Returns:
the fully initialized aggregate
Throws:
AggregateDeletedException - in case an aggregate existed in the past, but has been deleted
AggregateNotFoundException - when an aggregate with the given identifier does not exist

getAggregateFactory

public AggregateFactory<T> getAggregateFactory()
Returns the factory used by this repository.

Returns:
the factory used by this repository

resolveConflicts

protected void resolveConflicts(T aggregate,
                                DomainEventStream unseenEvents)
Resolve (potential) conflicts for the given aggregate, where given unseenEvents may have been concurrently applied.

Parameters:
aggregate - The aggregate containing the potential conflicts
unseenEvents - The events that have been concurrently applied

getTypeIdentifier

public String getTypeIdentifier()
Return the type identifier belonging to the AggregateFactory of this repository.

Returns:
the type identifier belonging to the AggregateFactory of this repository

validateOnLoad

protected void validateOnLoad(T aggregate,
                              Long expectedVersion)
Checks the aggregate for concurrent changes. Throws a ConflictingModificationException when conflicting changes have been detected.

This implementation throws a ConflictingAggregateVersionException if the expected version is not null and the version number of the aggregate does not match the expected version

This implementation will do nothing if a conflict resolver (See setConflictResolver(ConflictResolver) is set. Otherwise, it will call super.validateOnLoad(...).

Overrides:
validateOnLoad in class AbstractRepository<T extends EventSourcedAggregateRoot>
Parameters:
aggregate - The loaded aggregate
expectedVersion - The expected version of the aggregate

setEventStreamDecorators

public void setEventStreamDecorators(List<? extends EventStreamDecorator> eventProcessors)
Sets the Event Stream Decorators that will process the event in the DomainEventStream when read, or written to the event store.

When appending events to the event store, the processors are invoked in the reverse order, causing the first decorator in this list to receive each event first. When reading from events, the decorators are invoked in the order given.

Parameters:
eventProcessors - The processors to that will process events in the DomainEventStream

setSnapshotterTrigger

public void setSnapshotterTrigger(SnapshotterTrigger snapshotterTrigger)
Sets the snapshotter trigger for this repository.

Parameters:
snapshotterTrigger - the snapshotter trigger for this repository.

setConflictResolver

public void setConflictResolver(ConflictResolver conflictResolver)
Sets the conflict resolver to use for this repository. If not set (or null), the repository will throw an exception if any unexpected changes appear in loaded aggregates.

Parameters:
conflictResolver - The conflict resolver to use for this repository


Copyright © 2010-2016. All Rights Reserved.