Interface Aggregate<T>

Type Parameters:
T - The aggregate root type
All Known Implementing Classes:
AnnotatedAggregate, EventSourcedAggregate, LockAwareAggregate

public interface Aggregate<T>
Interface that describes an aggregate. An aggregate is an isolated tree of entities that is capable of handling commands. Implementations of this interface defer the actual handling of commands to a wrapped instance of type T or one of its entities.

When a command is dispatched to an aggregate Axon will load the aggregate instance and invoke the related command handler method. It is rarely necessary to interact with aggregates directly. Though it is not recommended it is possible to invoke methods on the wrapped instance using invoke(Function) and execute(Consumer).

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    execute(Consumer<T> invocation)
    Execute a method on the underlying aggregate or one of its instances.
    handle(Message<?> message)
    Handle the given message on the aggregate root or one of its child entities.
    Get the unique identifier of this aggregate
    default String
    Get the unique identifier of this aggregate, represented as a String.
    <R> R
    invoke(Function<T,R> invocation)
    Invoke a method on the underlying aggregate root or one of its instances.
    boolean
    Check if this aggregate has been deleted.
    Class<? extends T>
    Get the class type of the wrapped aggregate root that the Aggregate defers to for command handling.
    Get the String representation of the aggregate's type.
    Get the aggregate's version.
  • Method Details

    • type

      String type()
      Get the String representation of the aggregate's type. This defaults to the simple name of the rootType() unless configured otherwise.
      Returns:
      The aggregate's type
    • identifierAsString

      default String identifierAsString()
      Get the unique identifier of this aggregate, represented as a String.
      Returns:
      The aggregate's identifier as a String
    • identifier

      Object identifier()
      Get the unique identifier of this aggregate
      Returns:
      The aggregate's identifier
    • version

      Long version()
      Get the aggregate's version. For event sourced aggregates this is identical to the sequence number of the last applied event.
      Returns:
      The aggregate's version
    • handle

      Object handle(Message<?> message) throws Exception
      Handle the given message on the aggregate root or one of its child entities.
      Parameters:
      message - The message to be handled by the aggregate
      Returns:
      The result of message handling. Might returns null if for example handling a CommandMessage yields no results
      Throws:
      Exception - in case one is triggered during message processing
    • invoke

      <R> R invoke(Function<T,R> invocation)
      Invoke a method on the underlying aggregate root or one of its instances. Use this over execute(Consumer) to obtain an invocation result, for instance in order to query the aggregate.

      Note that the use of this method is not recommended as aggregates are not meant to be queried. Relying on this method is commonly a sign of design smell.

      Type Parameters:
      R - The type of the result produced by the given invocation
      Parameters:
      invocation - The function that performs the actual invocation
      Returns:
      The invocation result
    • execute

      void execute(Consumer<T> invocation)
      Execute a method on the underlying aggregate or one of its instances.

      Note that the use of this method is not recommended as the wrapped aggregate instance is not meant to be exposed. Relying on this method is commonly a sign of design smell.

      Parameters:
      invocation - The function that performs the invocation
    • isDeleted

      boolean isDeleted()
      Check if this aggregate has been deleted. This is checked by aggregate repositories when an aggregate is loaded. In case the repository is asked to load a deleted aggregate the repository will refuse by throwing an AggregateDeletedException.
      Returns:
      true in case the aggregate was deleted, false otherwise
    • rootType

      Class<? extends T> rootType()
      Get the class type of the wrapped aggregate root that the Aggregate defers to for command handling.
      Returns:
      The aggregate root type