Interface EntityMetamodelBuilder<E>
- Type Parameters:
E- The type of the entity modeled by this interface.
- All Known Subinterfaces:
PolymorphicEntityMetamodelBuilder<E>
EntityMetamodel instance, allowing the registration of command handlers, an entity evolver,
and child entities.
Entity models can handle two types of commands:
- Instance commands: Commands that are handled by an existing entity. These commands are registered by the
instanceCommandHandler(QualifiedName, EntityCommandHandler)method and will be invoked throughEntityMetamodel.handleInstance(CommandMessage, Object, ProcessingContext). These are comparable with static methods on an entity. - Creational commands: Commands that are handled by a new entity. These commands are registered by the
creationalCommandHandler(QualifiedName, CommandHandler)method and will be invoked throughEntityMetamodel.handleCreate(CommandMessage, ProcessingContext). These are comparable with class instance methods on an entity.
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:
- If an instance command is invoked for a non-existing entity, an
EntityMissingForInstanceCommandHandlerExceptionwill be thrown. - If a creational command is invoked for an existing entity, an
EntityAlreadyExistsForCreationalCommandHandlerExceptionwill be thrown.
- Since:
- 5.0.0
- Author:
- Mitchell Herrijgers
-
Method Summary
Modifier and TypeMethodDescriptionaddChild(EntityChildMetamodel<?, E> child) Adds aEntityChildMetamodelto this metamodel.build()Builds theEntityMetamodelinstance based on the configuration of this builder.creationalCommandHandler(QualifiedName qualifiedName, CommandHandler messageHandler) Adds aCommandHandlerto this metamodel for the givenQualifiedNamethat is in charge of creation of the entity.entityEvolver(EntityEvolver<E> entityEvolver) Adds aEntityEvolverto this metamodel.instanceCommandHandler(QualifiedName qualifiedName, EntityCommandHandler<E> messageHandler) Adds anEntityCommandHandlerto this metamodel for the givenQualifiedName.
-
Method Details
-
instanceCommandHandler
@Nonnull EntityMetamodelBuilder<E> instanceCommandHandler(@Nonnull QualifiedName qualifiedName, @Nonnull EntityCommandHandler<E> messageHandler) Adds anEntityCommandHandlerto this metamodel for the givenQualifiedName. The command handler will be invoked when a command with the givenQualifiedNameis 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 givenqualifiedNamewhen the entity is not yet created.You can register the same
QualifiedNamefor both instance and creational command handlers. See theEntityMetamodelBuilderclass documentation for more information on how this works.- Parameters:
qualifiedName- TheQualifiedNameof the command this handler handles.messageHandler- TheEntityCommandHandlerto handle the command.- Returns:
- This builder for further configuration.
-
creationalCommandHandler
@Nonnull EntityMetamodelBuilder<E> creationalCommandHandler(@Nonnull QualifiedName qualifiedName, @Nonnull CommandHandler messageHandler) Adds aCommandHandlerto this metamodel for the givenQualifiedNamethat 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 givenqualifiedNamewhen the entity is not yet created.You can register the same
QualifiedNamefor both instance and creational command handlers. See theEntityMetamodelBuilderclass 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- TheQualifiedNameof the command this handler handles.messageHandler- TheCommandHandlerto handle the command.- Returns:
- This builder for further configuration.
-
addChild
Adds aEntityChildMetamodelto 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:
- Single instances: For a field with a single instance, use the
EntityChildMetamodel.single(Class, EntityMetamodel). - List instances: For a
list, use theEntityChildMetamodel.list(Class, EntityMetamodel).
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- TheEntityChildMetamodelto add.- Returns:
- This builder for further configuration.
- Single instances: For a field with a single instance, use the
-
entityEvolver
Adds aEntityEvolverto 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- TheEntityEvolverto use.- Returns:
- This builder for further configuration.
-
build
Builds theEntityMetamodelinstance based on the configuration of this builder. This method should be called after all configuration is done.- Returns:
- The
EntityMetamodelinstance based on the configuration of this builder.
-