Interface EventSourcedEntityFactory<ID,E>
- Type Parameters:
ID- The type of the identifier of the entity to create.E- The type of the entity to create.
- All Known Implementing Classes:
AnnotationBasedEventSourcedEntityFactory
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
EventSourcingRepository should construct an entity of type E.
When sourcing an entity, the state is initially null. The first event will initialize the entity through
create(Object, EventMessage, ProcessingContext).
If no events are found during sourcing, the repository will return null for the entity if
EventSourcingRepository.load(Object, ProcessingContext) was used. However, if
EventSourcingRepository.loadOrCreate(Object, ProcessingContext) was used, the repository will return an empty
entity through calling create(Object, EventMessage, ProcessingContext) with a null
firstEventMessage. This may return null in case the entity should be created with the first event message
only. For example, when it is immutable.
Depending on the type of entity, the factory should be created differently. The following types of entities are supported:
-
Mutable entities: these entities are created with a no-argument constructor. All command handlers of the
entity should be
instance command handlers. UsefromNoArgument(Supplier)to create a factory for this type of entity. -
Mutable entities with non-null identifier: these entities are created with a constructor that takes the
identifier as parameter. All command handlers of the entity should be
instance command handlers. UsefromIdentifier(Function)to create a factory for this type of entity. -
Immutable entities, or entities with non-nullable parameters: these entities are created with a constructor that takes
the identifier and the event message as parameters. The entity should have a combination of
creational command handlersto create the entity if no events exist for it, andinstance command handlersto handle commands when it does exist. If a command could potentially handle both cases, it would need to be registered as both a creational and instance command handler. UsefromEventMessage(BiFunction)to create a factory for this type of entity.
Implementations should be thread-safe.
- Since:
- 5.0.0
- Author:
- Mitchell Herrijgers
-
Method Summary
Modifier and TypeMethodDescriptioncreate(ID id, EventMessage firstEventMessage, ProcessingContext context) Creates an entity of typeEwith the given identifier.static <ID,E> EventSourcedEntityFactory <ID, E> fromEventMessage(BiFunction<ID, EventMessage, E> creator) Creates a factory for an entity of typeEventSourcedEntityFactoryusing a specified constructor with the identifier and the event message as parameters.static <ID,E> EventSourcedEntityFactory <ID, E> fromIdentifier(Function<ID, E> creator) Creates a factory for an entity of typeEventSourcedEntityFactoryusing a specified constructor with the identifier as parameter.static <ID,E> EventSourcedEntityFactory <ID, E> fromNoArgument(Supplier<E> creator) Creates a factory for an entity of typeEventSourcedEntityFactoryusing a specified no-argument constructor.
-
Method Details
-
create
@Nullable E create(@Nonnull ID id, @Nullable EventMessage firstEventMessage, @Nonnull ProcessingContext context) Creates 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
fromNoArgument(Supplier),fromIdentifier(Function)orfromEventMessage(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.- 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.
-
fromNoArgument
Creates a factory for an entity of typeEventSourcedEntityFactoryusing a specified no-argument constructor.Should be used when your entity is mutable, and you want to create it with a no-argument constructor. All command handlers of your entity should be
instance command handler. If you would like the identifier to be passed to the constructor, usefromIdentifier(Function)instead.- Type Parameters:
ID- The type of the identifier of the entity.E- The type of the entity.- Parameters:
creator- ASupplierthat creates the entity. This should be a no-argument constructor.- Returns:
- A factory that creates the entity using the no-argument constructor.
-
fromIdentifier
Creates a factory for an entity of typeEventSourcedEntityFactoryusing a specified constructor with the identifier as parameter.Should be used when your entity is mutable, and you want to create it with a constructor that takes the identifier as parameter. All command handlers of your entity should be
instance command handler.- Type Parameters:
ID- The type of the identifier of the entity.E- The type of the entity.- Parameters:
creator- AFunctionthat creates the entity. This should be a constructor with the identifier as parameter.- Returns:
- A factory that creates the entity using the constructor with the identifier as parameter.
-
fromEventMessage
static <ID,E> EventSourcedEntityFactory<ID,E> fromEventMessage(@Nonnull BiFunction<ID, EventMessage, E> creator) Creates a factory for an entity of typeEventSourcedEntityFactoryusing a specified constructor with the identifier and the event message as parameters.Should be used if your entity is immutable, and/or you want to create it with a constructor that takes the identifier and the event message as parameters to set non-nullable parameters on it. Your entity should have a combination of
creational command handlersto create the entity if no events exist for it, andinstance command handlersto handle commands when it does exist. If a command could potentially handle both cases, it would need to be registered as both a creational and instance command handler.- Type Parameters:
ID- The type of the identifier of the entity.E- The type of the entity.- Parameters:
creator- ABiFunctionthat creates the entity. This should be a constructor with the identifier and the event as parameters.- Returns:
- A factory that creates the entity using the constructor with the identifier and the event message as parameters.
-