Class PolymorphicEntityMetamodel<E>
- Type Parameters:
E- The type of polymorphic entity this metamodel represents.
- All Implemented Interfaces:
DescribableComponent,EntityMetamodel<E>,EntityEvolver<E>
EntityMetamodel that represents an entity that can have multiple concrete types. For example,
Employee and Customer could be two concrete types of Person, sharing properties and a set of
commands and events.
This metamodel delegates commands to the concrete type if the concrete type is registered for the command. If not, it will attempt to handle the command with the super type. Concrete types thus take precedence over the super type for commands.
Events are delegates to both the super type and the concrete type.
- Since:
- 5.0.0
- Author:
- Mitchell Herrijgers
-
Method Summary
Modifier and TypeMethodDescriptionvoiddescribeTo(ComponentDescriptor descriptor) Describe the properties ofthis DescribableComponentwith the givendescriptor.Returns theClassof the entity this metamodel describes.evolve(E entity, EventMessage event, ProcessingContext context) Evolve the givenentityby applying the giveneventto it.static <E> PolymorphicEntityMetamodelBuilder<E> forSuperType(Class<E> entityType) Creates a new polymorphicEntityMetamodelfor the given super type.handleCreate(CommandMessage message, ProcessingContext context) Handles the givenCommandMessageas the creation of a new entity.handleInstance(CommandMessage message, E entity, ProcessingContext context) Handles the givenCommandMessagefor the givenentity.Returns the set of allQualifiedNamesthat this metamodel supports for command handlers, both creational and instance commands.Returns the set of allQualifiedNamesthat this metamodel supports for creating entities.Returns the set of allQualifiedNamesthat this metamodel supports for instance commands.toString()
-
Method Details
-
forSuperType
Creates a new polymorphicEntityMetamodelfor the given super type. The metamodel can then be used to add concrete types to the metamodel. Any method inherited fromEntityMetamodelBuilderis delegated to the super type metamodel.- Type Parameters:
E- The type of the entity to create a metamodel for.- Parameters:
entityType- The type of the entity to create a metamodel for.- Returns:
- A new
PolymorphicEntityMetamodel.Builderfor the given entity type.
-
evolve
Description copied from interface:EntityEvolverEvolve the givenentityby applying the giveneventto it.- Specified by:
evolvein interfaceEntityEvolver<E>- Parameters:
entity- The current entity to evolve with the givenevent.event- The event that might adjust theentity.context- The context within which to evolve theentityby the givenevent.- Returns:
- The evolved
entitybased on the givenevent, or the sameentitywhen nothing happened.
-
supportedCommands
Description copied from interface:EntityMetamodelReturns the set of allQualifiedNamesthat this metamodel supports for command handlers, both creational and instance commands. This is the union of theEntityMetamodel.supportedCreationalCommands()andEntityMetamodel.supportedInstanceCommands()methods.- Specified by:
supportedCommandsin interfaceEntityMetamodel<E>- Returns:
- A set of
QualifiedNameinstances representing the supported command names.
-
supportedCreationalCommands
Description copied from interface:EntityMetamodelReturns the set of allQualifiedNamesthat this metamodel supports for creating entities. These are the command types that can be used to create an entity of this type through theEntityMetamodel.handleCreate(org.axonframework.messaging.commandhandling.CommandMessage, org.axonframework.messaging.core.unitofwork.ProcessingContext)method.- Specified by:
supportedCreationalCommandsin interfaceEntityMetamodel<E>- Returns:
- A set of
QualifiedNameinstances representing the supported command names.
-
supportedInstanceCommands
Description copied from interface:EntityMetamodelReturns the set of allQualifiedNamesthat this metamodel supports for instance commands. These are the command types that can be used on entity instances of this type through theEntityMetamodel.handleInstance(org.axonframework.messaging.commandhandling.CommandMessage, E, org.axonframework.messaging.core.unitofwork.ProcessingContext)method.- Specified by:
supportedInstanceCommandsin interfaceEntityMetamodel<E>- Returns:
- A set of
QualifiedNameinstances representing the supported command names.
-
handleCreate
@Nonnull public MessageStream.Single<CommandResultMessage> handleCreate(@Nonnull CommandMessage message, @Nonnull ProcessingContext context) Description copied from interface:EntityMetamodelHandles the givenCommandMessageas the creation of a new entity. It is up to the registered command handler to create the entity.This method is used to handle commands for new entities. If you want to handle commands for existing entities, use the
EntityMetamodel.handleInstance(org.axonframework.messaging.commandhandling.CommandMessage, E, org.axonframework.messaging.core.unitofwork.ProcessingContext)method instead. If the command handler is only known as an instance command handler and this method is called, it will result in a failed message stream.- Specified by:
handleCreatein interfaceEntityMetamodel<E>- Parameters:
message- TheCommandMessageto handle.context- TheProcessingContextfor the command.- Returns:
- A stream with a message containing the result of the command handling, which may be a
CommandResultMessageor an error message.
-
handleInstance
@Nonnull public MessageStream.Single<CommandResultMessage> handleInstance(@Nonnull CommandMessage message, @Nonnull E entity, @Nonnull ProcessingContext context) Description copied from interface:EntityMetamodelHandles the givenCommandMessagefor the givenentity. If any of its children can handle the command, it will be delegated to them. Otherwise, the command will be handled by this metamodel. If the command is not handled by this metamodel or any of its children, anMessageStream.failed(Throwable)will be returned.This method is used to handle commands for existing entities. If you want to handle commands for new entities, use the
EntityMetamodel.handleCreate(org.axonframework.messaging.commandhandling.CommandMessage, org.axonframework.messaging.core.unitofwork.ProcessingContext)method instead. If the command handler is only known as a creational command handler and this method is called, it will result in a failed message stream.- Specified by:
handleInstancein interfaceEntityMetamodel<E>- Parameters:
message- TheCommandMessageto handle.entity- The entity instance to handle the command for.context- TheProcessingContextfor the command.- Returns:
- A stream with a message containing the result of the command handling, which may be a
CommandResultMessageor an error message.
-
entityType
Description copied from interface:EntityMetamodelReturns theClassof the entity this metamodel describes.- Specified by:
entityTypein interfaceEntityMetamodel<E>- Returns:
- The
Classof the entity this metamodel describes.
-
describeTo
Description copied from interface:DescribableComponentDescribe the properties ofthis DescribableComponentwith the givendescriptor.Components should call the appropriate
describePropertymethods 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
DescribableComponentimplementation 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 thedescribeTomethod, 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:
describeToin interfaceDescribableComponent- Parameters:
descriptor- The component descriptor to describethis DescribableComponentn its properties in.
-
toString
-