Class AbstractAggregateFactory<T>

java.lang.Object
org.axonframework.eventsourcing.AbstractAggregateFactory<T>
Type Parameters:
T - The type of Aggregate created by this factory
All Implemented Interfaces:
AggregateFactory<T>
Direct Known Subclasses:
GenericAggregateFactory

public abstract class AbstractAggregateFactory<T> extends Object implements AggregateFactory<T>
Abstract AggregateFactory implementation that is aware of snapshot events. If an incoming event is not a snapshot event, creation is delegated to the subclass.
Since:
2.0
Author:
Allard Buijze
  • Constructor Details

    • AbstractAggregateFactory

      protected AbstractAggregateFactory(Class<T> aggregateBaseType)
      Initialize an AggregateFactory for the given aggregateBaseType.

      If a first event is an instance of this aggregateBaseType, it is recognised as a snapshot event. Otherwise, the subclass is asked to instantiate a new aggregate root instance based on the first event.

      Parameters:
      aggregateBaseType - the base type of the aggregate roots created by this instance
    • AbstractAggregateFactory

      protected AbstractAggregateFactory(Class<T> aggregateBaseType, Set<Class<? extends T>> aggregateSubTypes)
      Initialize an AggregateFactory for the given polymorphic aggregateBaseType and it's aggregateSubTypes.

      If a first event is an instance of this aggregateBaseType, it is recognised as a snapshot event. Otherwise, the subclass is asked to instantiate a new aggregate root instance based on the first event.

      Parameters:
      aggregateBaseType - the base type of the aggregate roots created by this instance
      aggregateSubTypes - a Set of sub types of the given aggregateBaseType
    • AbstractAggregateFactory

      protected AbstractAggregateFactory(AggregateModel<T> aggregateModel)
      Initializes an AggregateFactory for the given aggregateModel.

      If a first event is an instance of any aggregate root within this aggregateModel, it is recognised as a snapshot event. Otherwise, the subclass is asked to instantiate a new aggregate root instance based on the first event.

      Parameters:
      aggregateModel - the model of aggregate to be created by this factory
  • Method Details

    • aggregateModel

      protected AggregateModel<T> aggregateModel()
      Gets the aggregate model.
      Returns:
      the aggregate model
    • createAggregateRoot

      public final T createAggregateRoot(String aggregateIdentifier, DomainEventMessage<?> firstEvent)
      Description copied from interface: AggregateFactory
      Instantiate the aggregate root using the given aggregate identifier and first event. The first event of the event stream is passed to allow the factory to identify the actual implementation type of the aggregate to create. The first event can be either the event that created the aggregate or, when using event sourcing, a snapshot event. In either case, the event should be designed, such that these events contain enough information to deduct the actual aggregate type.
      Specified by:
      createAggregateRoot in interface AggregateFactory<T>
      Parameters:
      aggregateIdentifier - the aggregate identifier of the aggregate to instantiate
      firstEvent - The first event in the event stream. This is either the event generated during creation of the aggregate, or a snapshot event
      Returns:
      an aggregate root ready for initialization using a DomainEventStream.
    • postProcessInstance

      protected T postProcessInstance(T aggregate)
      Perform any processing that must be done on an aggregate instance that was reconstructed from a Snapshot Event. Implementations may choose to modify the existing instance, or return a new instance.

      This method can be safely overridden. This implementation does nothing.

      Parameters:
      aggregate - The aggregate to post-process.
      Returns:
      The aggregate to initialize with the Event Stream
    • doCreateAggregate

      protected abstract T doCreateAggregate(String aggregateIdentifier, DomainEventMessage firstEvent)
      Create an uninitialized Aggregate instance with the given aggregateIdentifier. The given firstEvent can be used to define the requirements of the aggregate to create.

      The given firstEvent is never a snapshot event.

      Parameters:
      aggregateIdentifier - The identifier of the aggregate to create
      firstEvent - The first event in the Event Stream of the Aggregate
      Returns:
      The aggregate instance to initialize with the Event Stream
    • getAggregateType

      public Class<T> getAggregateType()
      Description copied from interface: AggregateFactory
      Returns the type of aggregate this factory creates. All instances created by this factory must be an instanceOf this type.
      Specified by:
      getAggregateType in interface AggregateFactory<T>
      Returns:
      The type of aggregate created by this factory