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

java.lang.Object
  extended by org.axonframework.eventsourcing.GenericAggregateFactory<T>
Type Parameters:
T - The type of aggregate this factory creates
All Implemented Interfaces:
AggregateFactory<T>

public class GenericAggregateFactory<T extends EventSourcedAggregateRoot>
extends Object
implements AggregateFactory<T>

Aggregate factory that uses a convention to create instances of aggregates. The aggregate must have a constructor with a single parameter: the AggregateIdentifier. This constructor should not do any initialization. The type identifier for the aggregate is the simple name (class name without the package) of the aggregate type.

Since:
0.7
Author:
Allard Buijze

Constructor Summary
GenericAggregateFactory(Class<T> aggregateType)
          Initialize the AggregateFactory for creating instances of the given aggregateType.
 
Method Summary
 T createAggregate(AggregateIdentifier aggregateIdentifier, DomainEvent firstEvent)
          Instantiate the aggregate using the given aggregate identifier and first event.
 String getTypeIdentifier()
          Returns the type identifier for this aggregate factory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericAggregateFactory

public GenericAggregateFactory(Class<T> aggregateType)
Initialize the AggregateFactory for creating instances of the given aggregateType.

Parameters:
aggregateType - The type of aggregate this factory creates instances of.
Throws:
IncompatibleAggregateException - if the aggregate constructor throws an exception, or if the JVM security settings prevent the GenericEventSourcingRepository from calling the constructor.
Method Detail

createAggregate

public T createAggregate(AggregateIdentifier aggregateIdentifier,
                         DomainEvent firstEvent)
Instantiate the aggregate 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.

This implementation is AggregateSnapshot aware. If the first event is an AggregateSnapshot, the aggregate is retrieved from the snapshot, instead of creating a new -blank- instance.

Specified by:
createAggregate in interface AggregateFactory<T extends EventSourcedAggregateRoot>
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 ready for initialization using a DomainEventStream.
Throws:
IncompatibleAggregateException - if the aggregate constructor throws an exception, or if the JVM security settings prevent the GenericEventSourcingRepository from calling the constructor.

getTypeIdentifier

public String getTypeIdentifier()
Description copied from interface: AggregateFactory
Returns the type identifier for this aggregate factory. The type identifier is used by the EventStore to organize data related to the same type of aggregate.

Tip: in most cases, the simple class name would be a good start.

Specified by:
getTypeIdentifier in interface AggregateFactory<T extends EventSourcedAggregateRoot>
Returns:
the type identifier of the aggregates this repository stores


Copyright © 2011. All Rights Reserved.