Class AnnotatedEntityMetamodel<E>
- Type Parameters:
E- The type of entity this metamodel describes.
- All Implemented Interfaces:
DescribableComponent,EntityMetamodel<E>,EntityEvolver<E>
EntityMetamodel implementation that uses reflection to inspect the entity. It will detect annotated
command- and event-handling methods, as well as child entities annotated with EntityMember. Everything that
is discovered is then registered to a delegate EntityMetamodel, so that essentially a declared metamodel is
built of which it's structure is clearly defined.
Besides normal EntityMetamodel operations, this metamodel also provides a means to
get the expected representation of a command or event handler based
on the QualifiedName of the message type. This is useful for determining the payload type of a command or
event handler when multiple handlers are present for the same message type.
NOTE: This class is a complete rewrite of the pre-5.0.0
org.axonframework.modelling.command.inspection.AnnotatedAggregateMetaModelFactory. Both scan the class for
annotated methods and fields, but the AnnotatedEntityModel dropped aggregate versioning (conflict resolution), no
longer required an id in the entity, and creates a declarative metamodel instead of relying on reflection at
runtime.
- Since:
- 3.1.0
- Author:
- Mitchell Herrijgers, Allard Buijze
-
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> AnnotatedEntityMetamodel<E> forConcreteType(Class<E> entityType, ParameterResolverFactory parameterResolverFactory, MessageTypeResolver messageTypeResolver, MessageConverter messageConverter, EventConverter eventConverter) Instantiate an annotatedEntityMetamodelof a concrete entity type.static <E> AnnotatedEntityMetamodel<E> forPolymorphicType(Class<E> entityType, Set<Class<? extends E>> concreteTypes, ParameterResolverFactory parameterResolverFactory, MessageTypeResolver messageTypeResolver, MessageConverter messageConverter, EventConverter eventConverter) Instantiate an annotatedEntityMetamodelof a polymorphic entity type.Class<?> getExpectedRepresentation(QualifiedName qualifiedName) Returns theClassof the expected representation for handlers of the givenqualifiedName.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.
-
Method Details
-
forConcreteType
public static <E> AnnotatedEntityMetamodel<E> forConcreteType(@Nonnull Class<E> entityType, @Nonnull ParameterResolverFactory parameterResolverFactory, @Nonnull MessageTypeResolver messageTypeResolver, @Nonnull MessageConverter messageConverter, @Nonnull EventConverter eventConverter) Instantiate an annotatedEntityMetamodelof a concrete entity type.- Type Parameters:
E- The type of entity this metamodel describes.- Parameters:
entityType- The concrete entity type this metamodel describes.parameterResolverFactory- TheParameterResolverFactoryto use for resolving parameters.messageTypeResolver- TheMessageTypeResolverto use for resolving message types from payload classes.messageConverter- The converter used to convert theMessage.payload()to the desired format.eventConverter- The converter used to convert theMessage.payload()to the desired format.- Returns:
- An annotated
EntityMetamodelbacked by aConcreteEntityMetamodelfor the given entity type.
-
forPolymorphicType
public static <E> AnnotatedEntityMetamodel<E> forPolymorphicType(@Nonnull Class<E> entityType, @Nonnull Set<Class<? extends E>> concreteTypes, @Nonnull ParameterResolverFactory parameterResolverFactory, @Nonnull MessageTypeResolver messageTypeResolver, @Nonnull MessageConverter messageConverter, @Nonnull EventConverter eventConverter) Instantiate an annotatedEntityMetamodelof a polymorphic entity type. At least one concrete type must be supplied, as this metamodel is meant to describe a polymorphic entity type with multiple concrete implementations.- Type Parameters:
E- The type of the polymorphic entity.- Parameters:
entityType- The polymorphic entity type this metamodel describes.concreteTypes- The concrete types of the polymorphic entity type.parameterResolverFactory- TheParameterResolverFactoryto use for resolving parameters.messageTypeResolver- TheMessageTypeResolverto use for resolving message types from payload classes.messageConverter- The converter used to convert theMessage.payload()to the desired format.eventConverter- The event converter used to convert theMessage.payload()to the desired format.- Returns:
- An annotated
EntityMetamodelbacked by aPolymorphicEntityMetamodelfor the given entity type.
-
getExpectedRepresentation
Returns theClassof the expected representation for handlers of the givenqualifiedName.- Parameters:
qualifiedName- TheQualifiedNameof the handler to look for.- Returns:
- The
Classof the expected representation for handlers of the givenqualifiedName, ornullif no such representation is found.
-
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.
-
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.
-
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.
-
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.
-