Class KeyValueEntity<S>
- Type Parameters:
S
- The type of the state for this entity.
When implementing a Key Value Entity, you first define what will be its internal state (your domain model), and the commands it will handle (mutation requests).
Each command is handled by a command handler. Command handlers are methods returning an KeyValueEntity.Effect
.
When handling a command, you use the Effect API to:
- update the entity state and send a reply to the caller
- directly reply to the caller if the command is not requesting any state change
- rejected the command by returning an error
- instruct the runtime to delete the entity
Concrete classes can accept the following types to the constructor:
KeyValueEntityContext
- Custom types provided by a
DependencyProvider
from the service setup
Concrete class must be annotated with ComponentId
.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
An Effect is a description of what the runtime needs to do after the command is handled. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected final CommandContext
Additional context and metadata for a command handler.protected final S
Returns the state as currently stored.protected final KeyValueEntity.Effect.Builder
<S> effects()
Implement by returning the initial empty state object.
-
Constructor Details
-
KeyValueEntity
public KeyValueEntity()
-
-
Method Details
-
emptyState
Implement by returning the initial empty state object. This object will be passed into the command handlers, until a new state replaces it.Also known as "zero state" or "neutral state".
The default implementation of this method returns
null
. It can be overridden to return a more sensible initial state. -
commandContext
Additional context and metadata for a command handler.It will throw an exception if accessed from constructor.
- Throws:
IllegalStateException
- if accessed outside a handler method
-
currentState
Returns the state as currently stored.Note that modifying the state directly will not update it in storage. To save the state, you must call {
effects().updateState()
}.This method can only be called when handling a command. Calling it outside a method (eg: in the constructor) will raise a IllegalStateException exception.
- Throws:
IllegalStateException
- if accessed outside a handler method
-
effects
-