Class InitializingEntityEvolver<I,E>

java.lang.Object
org.axonframework.eventsourcing.handler.InitializingEntityEvolver<I,E>
Type Parameters:
I - the type of the identifier of the entity to create
E - the type of the entity to create
All Implemented Interfaces:
DescribableComponent

@Internal public class InitializingEntityEvolver<I,E> extends Object implements DescribableComponent
Combines an EventSourcedEntityFactory and an EntityEvolver to ensure an entity exists and apply state transitions.

If the given entity is null, a new instance is created using the factory. The resulting entity is then passed to the evolver to apply the given event (if any).

This effectively provides "initialize or evolve" semantics.

Since:
5.1.0
Author:
John Hendrikx
  • Constructor Details

  • Method Details

    • initialize

      public E initialize(I identifier, ProcessingContext context)
      Creates an empty entity E.
      Parameters:
      identifier - the entity's identifier, cannot be null
      context - a ProcessingContext, cannot be null
      Returns:
      a new entity of type E
    • evolve

      public E evolve(I identifier, @Nullable E entity, EventMessage message, ProcessingContext context)
      Initializes or evolves the given entity using the given message. If the entity is null, creates the entity using the given message, otherwise evolves it.
      Parameters:
      identifier - the entity's identifier, cannot be null
      entity - the current state, can be null
      message - an event message to initialize or evolve the entity with, cannot be null
      context - a ProcessingContext, cannot be null
      Returns:
      an entity in its new state, never null
    • describeTo

      public void describeTo(ComponentDescriptor descriptor)
      Description copied from interface: DescribableComponent
      Describe the properties of this DescribableComponent with the given descriptor.

      Components should call the appropriate describeProperty methods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.

      Best Practices: As a general rule, all relevant fields of a DescribableComponent implementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing the describeTo method, who should focus on providing information that is useful for understanding the component's configuration and state.

      Example implementation:

       public void describeTo(ComponentDescriptor descriptor) {
           descriptor.describeProperty("name", this.name);
           descriptor.describeProperty("enabled", this.enabled);
           descriptor.describeProperty("configuration", this.configuration); // A nested component
           descriptor.describeProperty("handlers", this.eventHandlers);      // A collection
       }
       
      Specified by:
      describeTo in interface DescribableComponent
      Parameters:
      descriptor - The component descriptor to describe this DescribableComponentn its properties in.