Class AnnotationBasedEventSourcedEntityFactory<E,ID>
- Type Parameters:
E- The type of entity to create.ID- The type of identifier used by the entity.
- All Implemented Interfaces:
EventSourcedEntityFactory<ID,E>
EventSourcedEntityFactory interface. This factory will look for
EntityCreator-annotated constructors and static methods on the entity type and its supertypes to find a
suitable constructor or static method to create an entity instance.
This class implements the requirements as per the EntityCreator annotation. This class is thread-safe.
- Since:
- 5.0.0
- Author:
- Mitchell Herrijgers
-
Constructor Summary
ConstructorsConstructorDescriptionAnnotationBasedEventSourcedEntityFactory(Class<E> entityType, Class<ID> idType, Set<Class<? extends E>> subTypes, ParameterResolverFactory parameterResolverFactory, MessageTypeResolver messageTypeResolver, EventConverter converter) Instantiate a reflection-basedEventSourcedEntityFactoryfor the given superentityType, with the givensubTypes.AnnotationBasedEventSourcedEntityFactory(Class<E> entityType, Class<ID> idType, ParameterResolverFactory parameterResolverFactory, MessageTypeResolver messageTypeResolver, EventConverter converter) Instantiate an annotation-basedEventSourcedEntityFactoryfor the given concreteentityType. -
Method Summary
Modifier and TypeMethodDescriptioncreate(ID id, EventMessage firstEventMessage, ProcessingContext context) Creates an entity of typeEwith the given identifier.
-
Constructor Details
-
AnnotationBasedEventSourcedEntityFactory
public AnnotationBasedEventSourcedEntityFactory(@Nonnull Class<E> entityType, @Nonnull Class<ID> idType, @Nonnull ParameterResolverFactory parameterResolverFactory, @Nonnull MessageTypeResolver messageTypeResolver, @Nonnull EventConverter converter) Instantiate an annotation-basedEventSourcedEntityFactoryfor the given concreteentityType. When using a polymorphic entity type, you can use theAnnotationBasedEventSourcedEntityFactory(Class, Class, Set, ParameterResolverFactory, MessageTypeResolver, EventConverter), so that all subtypes of the entity type will be scanned for static methods and constructors.- Parameters:
entityType- The type of the entity to create. Must be concrete.idType- The type of the identifier used by the entity.parameterResolverFactory- The factory to use to resolve parameters.messageTypeResolver- The factory to use to resolve the payload type.converter- The converter to use for converting event payloads to the handler's expected type.
-
AnnotationBasedEventSourcedEntityFactory
public AnnotationBasedEventSourcedEntityFactory(@Nonnull Class<E> entityType, @Nonnull Class<ID> idType, @Nonnull Set<Class<? extends E>> subTypes, @Nonnull ParameterResolverFactory parameterResolverFactory, @Nonnull MessageTypeResolver messageTypeResolver, @Nonnull EventConverter converter) Instantiate a reflection-basedEventSourcedEntityFactoryfor the given superentityType, with the givensubTypes. ThesubTypesmust be concrete types that extend theentityType. The factory will look for static methods and constructors on thesubTypesand their supertypes to find a suitable constructor or static method to create an entity instance.- Parameters:
entityType- The type of the entity to create. Can be abstract.idType- The type of the identifier used by the entity.subTypes- The concrete types that extend theentityType.parameterResolverFactory- The factory to use to resolve parameters.messageTypeResolver- The factory to use to resolve the payload type.converter- The converter to use for converting event payloads to the handler's expected type.
-
-
Method Details
-
create
@Nullable public E create(@Nonnull ID id, @Nullable EventMessage firstEventMessage, @Nonnull ProcessingContext context) Description copied from interface:EventSourcedEntityFactoryCreates an entity of typeEwith the given identifier. The identifier is guaranteed to be non-null. The suppliedfirstEventMessageis the first event message that is present in the stream of the entity. If no event messages are present, this method will be called with anullfirstEventMessageto get an initial state when callingEventSourcingRepository.loadOrCreate(Object, ProcessingContext). UsingEventSourcingRepository.load(Object, ProcessingContext)would never call this method with anullfirstEventMessage.Invocations with a non-null
firstEventMessagemust always return a non-null entity, while invocations with a nullfirstEventMessagemay return null.Whether to return
nullfrom anullfirstEventMessageinvocation depends on the type of command handler which should be invoked when the entity does not exist. If this is acreational command handler, this should returnnull. If this is ainstance command handler, this should return the non-null initial state of the entity. For example, using the no-argument constructor of the entity, or a constructor that takes the identifier as a parameter.It is recommended to use
EventSourcedEntityFactory.fromNoArgument(Supplier),EventSourcedEntityFactory.fromIdentifier(Function)orEventSourcedEntityFactory.fromEventMessage(BiFunction)to create a factory that creates the entity based on the constructor of the entity. This will ensure that the right factory is created.- Specified by:
createin interfaceEventSourcedEntityFactory<E,ID> - Parameters:
id- The identifier of the entity to create. This is guaranteed to be non-null.firstEventMessage- The first event message that is present in the stream of the entity. This may benullif no event messages are present.context- The processing context.- Returns:
- The entity to create. This may be
nullif no entity should be created.
-