Class AggregateRootWithState<ID,EVENT_TYPE extends Event<ID>,STATE extends AggregateState<ID,EVENT_TYPE>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>
- java.lang.Object
-
- dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.classic.AggregateRoot<ID,EVENT_TYPE,AGGREGATE_TYPE>
-
- dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.classic.state.AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>
-
- Type Parameters:
ID- the aggregate id typeEVENT_TYPE- the type of eventAGGREGATE_TYPE- the aggregate self type (i.e. your concrete aggregate type)STATE- the aggregate state type (i.e. your concrete aggregate state)
- All Implemented Interfaces:
Aggregate<ID,AGGREGATE_TYPE>,StatefulAggregate<ID,EVENT_TYPE,AGGREGATE_TYPE>
public abstract class AggregateRootWithState<ID,EVENT_TYPE extends Event<ID>,STATE extends AggregateState<ID,EVENT_TYPE>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>> extends AggregateRoot<ID,EVENT_TYPE,AGGREGATE_TYPE>
Variant of theAggregateRootpattern where the aggregate's state and allEventHandlerannotated methods are placed within the concreteAggregateStateobject.
When theAggregateRootWithStateis combined withAggregateState, then theAggregateRootWithStatewill contain the command methods and theAggregateStatecontains the state fields and theEventHandlerannotated methods.
-
-
Constructor Summary
Constructors Constructor Description AggregateRootWithState()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description IDaggregateId()The id of the aggregate (aka.protected voidapply(EVENT_TYPE event)Apply a new non persisted/uncommitted Event to this aggregate instance.
If it is the very FIRSTEventthat is being applied thenEvent.aggregateId()MUST return the ID of the aggregate the event relates to
Every consecutiveEventapplied will have itsEvent.aggregateId(Object)method called IF it doesn't already have a value.dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.types.EventOrdereventOrderOfLastAppliedEvent()Get theof the last {}@link Eventthat was applied to theAggregateRoot(either usingAggregateRoot.rehydrate(AggregateEventStream)/AggregateRoot.rehydrate(Stream)or usingAggregateRoot.apply(Event)dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.types.EventOrdereventOrderOfLastRehydratedEvent()Get the eventOrder of the last event during aggregate hydration (using theAggregate.rehydrate(AggregateEventStream)method)EventsToPersist<ID,EVENT_TYPE>getUncommittedChanges()The the events that have been applied to this aggregate instance but not yet persisted to the underlyingEventStoreprotected voidinitialize()Override this method to initialize thestatevariable in case theresolveStateImplementationClass()doesn't fit the requirementsvoidmarkChangesAsCommitted()Resets theAggregateRoot.getUncommittedChanges()- effectively marking them as having been persisted and committed to the underlyingEventStoreAGGREGATE_TYPErehydrate(Stream<EVENT_TYPE> previousEvents)Effectively performs a leftFold over all the previous events related to this aggregate instanceprotected Class<AggregateState>resolveStateImplementationClass()Override this method to provide a non reflection based look up of the Type Argument provided to theAggregateRootWithStateclass-
Methods inherited from class dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.classic.AggregateRoot
applyEventToTheAggregateState, hasBeenRehydrated, isRehydrating, rehydrate
-
-
-
-
Field Detail
-
state
protected STATE extends AggregateState<ID,EVENT_TYPE> state
-
-
Method Detail
-
initialize
protected void initialize()
Override this method to initialize thestatevariable in case theresolveStateImplementationClass()doesn't fit the requirements- Overrides:
initializein classAggregateRoot<ID,EVENT_TYPE extends Event<ID>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>
-
rehydrate
public AGGREGATE_TYPE rehydrate(Stream<EVENT_TYPE> previousEvents)
Description copied from class:AggregateRootEffectively performs a leftFold over all the previous events related to this aggregate instance- Overrides:
rehydratein classAggregateRoot<ID,EVENT_TYPE extends Event<ID>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>- Parameters:
previousEvents- the previous events related to this aggregate instance, aka. the aggregates history- Returns:
- the same aggregate instance (self)
-
eventOrderOfLastRehydratedEvent
public dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.types.EventOrder eventOrderOfLastRehydratedEvent()
Description copied from interface:AggregateGet the eventOrder of the last event during aggregate hydration (using theAggregate.rehydrate(AggregateEventStream)method)- Specified by:
eventOrderOfLastRehydratedEventin interfaceAggregate<ID,EVENT_TYPE extends Event<ID>>- Overrides:
eventOrderOfLastRehydratedEventin classAggregateRoot<ID,EVENT_TYPE extends Event<ID>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>- Returns:
- the event order of the last applied
EventorEventOrder.NO_EVENTS_PREVIOUSLY_PERSISTEDin case no events has ever been applied to the aggregate
-
apply
protected void apply(EVENT_TYPE event)
Description copied from class:AggregateRootApply a new non persisted/uncommitted Event to this aggregate instance.
If it is the very FIRSTEventthat is being applied thenEvent.aggregateId()MUST return the ID of the aggregate the event relates to
Every consecutiveEventapplied will have itsEvent.aggregateId(Object)method called IF it doesn't already have a value. I.e. you can be lazy and skip setting the aggregate id on the Event if you don't want to.The
AggregateRootautomatically keeps track of theEvent.eventOrder()value and will set it for you and ensure that it's growing consecutively.- Overrides:
applyin classAggregateRoot<ID,EVENT_TYPE extends Event<ID>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>- Parameters:
event- the event to apply
-
aggregateId
public ID aggregateId()
Description copied from interface:AggregateThe id of the aggregate (aka. the stream-id)- Specified by:
aggregateIdin interfaceAggregate<ID,EVENT_TYPE extends Event<ID>>- Overrides:
aggregateIdin classAggregateRoot<ID,EVENT_TYPE extends Event<ID>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>
-
eventOrderOfLastAppliedEvent
public dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.types.EventOrder eventOrderOfLastAppliedEvent()
Description copied from class:AggregateRootGet theof the last {}@link Eventthat was applied to theAggregateRoot(either usingAggregateRoot.rehydrate(AggregateEventStream)/AggregateRoot.rehydrate(Stream)or usingAggregateRoot.apply(Event)- Overrides:
eventOrderOfLastAppliedEventin classAggregateRoot<ID,EVENT_TYPE extends Event<ID>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>- Returns:
- the event order of the last applied
EventorEventOrder.NO_EVENTS_PREVIOUSLY_PERSISTEDin case no events has ever been applied to the aggregate
-
getUncommittedChanges
public EventsToPersist<ID,EVENT_TYPE> getUncommittedChanges()
Description copied from class:AggregateRootThe the events that have been applied to this aggregate instance but not yet persisted to the underlyingEventStore- Specified by:
getUncommittedChangesin interfaceStatefulAggregate<ID,EVENT_TYPE extends Event<ID>,STATE extends AggregateState<ID,EVENT_TYPE>>- Overrides:
getUncommittedChangesin classAggregateRoot<ID,EVENT_TYPE extends Event<ID>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>- Returns:
- the changes to the aggregate
-
markChangesAsCommitted
public void markChangesAsCommitted()
Description copied from class:AggregateRootResets theAggregateRoot.getUncommittedChanges()- effectively marking them as having been persisted and committed to the underlyingEventStore- Specified by:
markChangesAsCommittedin interfaceStatefulAggregate<ID,EVENT_TYPE extends Event<ID>,STATE extends AggregateState<ID,EVENT_TYPE>>- Overrides:
markChangesAsCommittedin classAggregateRoot<ID,EVENT_TYPE extends Event<ID>,AGGREGATE_TYPE extends AggregateRootWithState<ID,EVENT_TYPE,STATE,AGGREGATE_TYPE>>
-
resolveStateImplementationClass
protected Class<AggregateState> resolveStateImplementationClass()
Override this method to provide a non reflection based look up of the Type Argument provided to theAggregateRootWithStateclass- Returns:
- the
AggregateStateimplementation to use for thestateinstance
-
-