org.axonframework.eventsourcing
Class SpringPrototypeEventSourcingRepository<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>
              extended by org.axonframework.eventsourcing.SpringPrototypeEventSourcingRepository<T>
Type Parameters:
T - The type of bean managed by this repository.
All Implemented Interfaces:
AggregateFactory<T>, Repository<T>, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware

public class SpringPrototypeEventSourcingRepository<T extends EventSourcedAggregateRoot>
extends EventSourcingRepository<T>
implements org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.BeanNameAware

Repository implementation that creates and initializes aggregates based on a Spring prototype bean. This allows aggregates to be easily configured and injected with Spring dependencies.

The prototypeBeanName should be set to the name of the bean that this repository should use as a prototype. It is required that the bean implements EventSourcedAggregateRoot, and it should have an accessible constructor that takes a single UUID argument.

Since:
0.6
Author:
Allard Buijze

Constructor Summary
SpringPrototypeEventSourcingRepository()
           
 
Method Summary
 void afterPropertiesSet()
           
 String getTypeIdentifier()
          Returns the type identifier for this aggregate factory.
protected  T instantiateAggregate(AggregateIdentifier aggregateIdentifier, DomainEvent firstEvent)
          Instantiate the aggregate using the given aggregate identifier and first event.
 void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
           
 void setBeanName(String name)
           
 void setPrototypeBeanName(String prototypeBeanName)
          Sets the name of the prototype bean this repository serves.
 void setTypeIdentifier(String typeIdentifier)
          Sets the type identifier of the aggregate served by this repository.
 
Methods inherited from class org.axonframework.eventsourcing.EventSourcingRepository
createAggregate, doLoad, doSaveWithLock, setConflictResolver, setEventStore, setEventStreamDecorators, setSnapshotterTrigger, validateOnLoad
 
Methods inherited from class org.axonframework.repository.LockingRepository
doSave, load
 
Methods inherited from class org.axonframework.repository.AbstractRepository
add, load, setEventBus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpringPrototypeEventSourcingRepository

public SpringPrototypeEventSourcingRepository()
Method Detail

instantiateAggregate

protected T instantiateAggregate(AggregateIdentifier aggregateIdentifier,
                                 DomainEvent firstEvent)
Description copied from class: EventSourcingRepository
Instantiate the aggregate using the given aggregate identifier and first event. The first event of the event stream is passed to allow the repository 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.

Note that aggregate state should *not* be initialized by this method. That means, no events should be applied by a call to this method. The first event is passed to allow the implementation to base the exact type of aggregate to instantiate on that event.

Specified by:
instantiateAggregate in class EventSourcingRepository<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.

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

setPrototypeBeanName

public void setPrototypeBeanName(String prototypeBeanName)
Sets the name of the prototype bean this repository serves. Note that the the bean should have the prototype scope and have a constructor that takes a single UUID argument.

Parameters:
prototypeBeanName - the name of the prototype bean this repository serves.

setTypeIdentifier

public void setTypeIdentifier(String typeIdentifier)
Sets the type identifier of the aggregate served by this repository. The type identifier is used to identify events in the event store as belonging to an aggregate served by this repository.

Defaults to the bean name of the prototype bean.

Parameters:
typeIdentifier - the type identifier of the aggregate served by this repository.

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
Exception

setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
                           throws org.springframework.beans.BeansException
Specified by:
setApplicationContext in interface org.springframework.context.ApplicationContextAware
Throws:
org.springframework.beans.BeansException

setBeanName

public void setBeanName(String name)
Specified by:
setBeanName in interface org.springframework.beans.factory.BeanNameAware


Copyright © 2011. All Rights Reserved.