Try another registration

Run the registration sample once to save Alice. Then change one input.

Ask to register Bob

In the sample's Program.fs / Program.cs, change the command to:

Shared setup
module Account =
    open FCQRS.Common
    open FCQRS.FSharp

    type RegisterUser = RegisterUser of name: string
    type UserRegistered = UserRegistered of name: string

    let decide (command: Command<RegisterUser>) (state: string option) =
        let (RegisterUser name) = command.CommandDetails
        persistIf state.IsNone (UserRegistered(defaultArg state name))

    let fold (event: Event<UserRegistered>) (_state: string option) =
        let (UserRegistered name) = event.EventDetails
        Some name
open System
open FCQRS.Model.Data
open FCQRS.Common
open FCQRS.FSharp
open Account
let send (accounts: AggregateHandle<RegisterUser, UserRegistered>) (id: AggregateId) = async {
    let! reply = accounts.Send (Fcqrs.newCid ()) id (RegisterUser "Bob") (fun _ -> true)
var reply = await accounts(_ => true, Values.NewCID(), id, new RegisterUser("Bob"));

Keep accountId set to "alice" and run the same project again:

Already registered: Alice (version 1)
Query: Alice

The handler uses the existing name when the account is already registered. Its conditional-persist call returns a deferred UserRegistered("Alice") reply. Applying that reply leaves state unchanged, and no second event is saved.

If the reply used "Bob" instead, applying it would change the in-memory name without recording the change. Restarting would recover "Alice" again. That is why the handler chooses the name before deciding whether to persist.

Create a different account

Keep the command's name as "Bob" and change the account ID near the top of Program:

Shared setup
    return reply
}
let accountId = "bob"
var accountId = "bob";

Run again:

Registered: Bob (version 1)
Query: Bob

alice and bob identify separate aggregate instances, each with its own state and event history. The account ID selects whose rules run; the name is the data that account stores.

Set the ID back to "alice" and run again: Alice is still registered at version 1. Restore the command's name to "Alice" afterward.

See how the query works.