Interface EntityMetamodelBuilder<E>

Type Parameters:
E - The type of the entity modeled by this interface.
All Known Subinterfaces:
PolymorphicEntityMetamodelBuilder<E>

public interface EntityMetamodelBuilder<E>
Builder for an EntityMetamodel instance, allowing the registration of command handlers, an entity evolver, and child entities.

Entity models can handle two types of commands:

You can also register the same QualifiedName for both instance and creational command handlers. In that case, the instance command handler will be invoked when EntityMetamodel.handleInstance(CommandMessage, Object, ProcessingContext) is invoked, and the creational command handler will be invoked when EntityMetamodel.handleCreate(CommandMessage, ProcessingContext) is invoked.

Upon a mismatch a RuntimeException will be thrown:

Since:
5.0.0
Author:
Mitchell Herrijgers
  • Method Details

    • instanceCommandHandler

      @Nonnull EntityMetamodelBuilder<E> instanceCommandHandler(@Nonnull QualifiedName qualifiedName, @Nonnull EntityCommandHandler<E> messageHandler)
      Adds an EntityCommandHandler to this metamodel for the given QualifiedName. The command handler will be invoked when a command with the given QualifiedName is received by the metamodel.

      The entity should not exist for this command handler to be invoked. A non-null initial state is considered to be an existing entity. As such, only register this command if the Repository.loadOrCreate(Object, ProcessingContext) will always return a non-null entity for the given qualifiedName when the entity is not yet created.

      You can register the same QualifiedName for both instance and creational command handlers. See the EntityMetamodelBuilder class documentation for more information on how this works.

      Parameters:
      qualifiedName - The QualifiedName of the command this handler handles.
      messageHandler - The EntityCommandHandler to handle the command.
      Returns:
      This builder for further configuration.
    • creationalCommandHandler

      @Nonnull EntityMetamodelBuilder<E> creationalCommandHandler(@Nonnull QualifiedName qualifiedName, @Nonnull CommandHandler messageHandler)
      Adds a CommandHandler to this metamodel for the given QualifiedName that is in charge of creation of the entity. The handler is expected to create the entity.

      The entity needs to not exist for this command handler to be invoked. A null initial state is considered to be a non-existing entity. As such, only register this command if the Repository.load(Object, ProcessingContext) will always return a null entity for the given qualifiedName when the entity is not yet created.

      You can register the same QualifiedName for both instance and creational command handlers. See the EntityMetamodelBuilder class documentation for more information on how this works.

      Note: If this metamodel is added as a child to another entity metamodel and has a creational command handler, it will result in an exception as child entities cannot be created through a creational command handler.

      Parameters:
      qualifiedName - The QualifiedName of the command this handler handles.
      messageHandler - The CommandHandler to handle the command.
      Returns:
      This builder for further configuration.
    • addChild

      @Nonnull EntityMetamodelBuilder<E> addChild(@Nonnull EntityChildMetamodel<?,E> child)
      Adds a EntityChildMetamodel to this metamodel. The child metamodel will be used to handle commands for the child entity. You can build a tree of entities by adding child metamodels to the parent metamodel. Children command handlers take precedence over the parent command handlers. Event handlers will be invoked on both the parent and child metamodels, but the child metamodels will be invoked first.

      There are various types of children that can be added to an entity metamodel:

      When multiple children that can handle the same command are present, the children will be filtered based on EntityChildMetamodel.canHandle(CommandMessage, Object, ProcessingContext), and thus only invoke the child with a matching entity. If no child can handle the command, an exception will be thrown. If after filtering, multiple children can handle the command, an exception will be thrown.

      Parameters:
      child - The EntityChildMetamodel to add.
      Returns:
      This builder for further configuration.
    • entityEvolver

      @Nonnull EntityMetamodelBuilder<E> entityEvolver(@Nullable EntityEvolver<E> entityEvolver)
      Adds a EntityEvolver to this metamodel. This evolver will be called upon applying an event to the entity. The evolver is responsible for evolving the entity state based on the event. Note that providing an evolver is optional. However, if no evolver is provided, the entity state can only be changed through command handlers.

      Calling this method a second time will override the previously set evolver.

      Parameters:
      entityEvolver - The EntityEvolver to use.
      Returns:
      This builder for further configuration.
    • build

      @Nonnull EntityMetamodel<E> build()
      Builds the EntityMetamodel instance based on the configuration of this builder. This method should be called after all configuration is done.
      Returns:
      The EntityMetamodel instance based on the configuration of this builder.