DurableStateBehavior
Fluent builder for durable-state actor behaviors — persists the full current state as a single snapshot on every write.
What it does
DurableStateBehavior is the simpler persistence model: rather than storing a sequence of domain events, it saves the actor's complete current state on each DurableEffect::persist() call. There is no event replay on startup — the DurableStateEngine loads the latest snapshot from the DurableStateStore and delivers it as the initial state before the first user command arrives. This trades event-history auditability for lower storage overhead and much faster recovery. Use DurableStateBehavior for read-model projections, session state, configuration caches, and any entity where an event log would add cost without adding value.
Example
use Monadial\Nexus\Persistence\PersistenceId;
use Monadial\Nexus\Persistence\State\DurableStateBehavior;
use Monadial\Nexus\Persistence\State\DurableEffect;
$behavior = DurableStateBehavior::create(
PersistenceId::of('UserProfile', $userId),
new UserProfile(),
static fn (ActorContext $ctx, object $cmd, UserProfile $state): DurableEffect => match (true) {
$cmd instanceof UpdateEmail => DurableEffect::persist($state->withEmail($cmd->email)),
$cmd instanceof UpdateName => DurableEffect::persist($state->withName($cmd->name)),
$cmd instanceof GetProfile => DurableEffect::none()
->thenReply($ctx->sender(), fn (UserProfile $s) => $s),
$cmd instanceof DeleteProfile => DurableEffect::persist(UserProfile::deleted())
->thenStop(),
default => DurableEffect::none(),
},
)
->withStateStore($stateStore)
->toBehavior();
Key methods
DurableStateBehavior::create(PersistenceId, object $emptyState, Closure $commandHandler): self— entry point; the three required arguments define the full durable-state contract.->withStateStore(DurableStateStore $store): self— required; the backing store for loading and persisting full state snapshots.->withWriterId(Ulid $writerId): self— override the ULID stamped on persisted envelopes; useful for deterministic tests or data-migration scenarios.->toBehavior(): Behavior— compile everything into aBehaviorready to pass toProps::fromBehavior(); throwsLogicExceptionifwithStateStore()was not called.
Full API reference
Full method list and class hierarchy
See also
- Persistence concept — durable state model, recovery pipeline, and comparison with event sourcing
- PersistenceId — how to construct the stable identity used as the primary key
- EventSourcedBehavior — event-sourced alternative with full history and snapshot strategies
- Behavior — the result type returned by
toBehavior()