Multiple firings of GetState on a single transition

86 views
Skip to first unread message

Ilya T

unread,
Jan 4, 2012, 1:17:37 AM1/4/12
to Stateless .NET State Machine Framework
Hi,

I'm trying out Sateless for the first time, and trying to understand
the persistance mechanism. When I'm running the below code, I'm
seeing 3 consecutive calls to GetState(). My concern is if I'm
persisting the state to a database, I don't want unnecessary queries.
Can someone shed light on this? Why is it firing multiple times?

Here's the self contained code, I'm using Stateless off the NuGet
repository (version 2.3.1.1)...
When you run the below code you'll see 3 consecutive calls to
GetState(), then SetState(), then GetState().

class Program
{
public enum State
{
A, B, C
}
public enum Trigger
{
AtoB, BtoC
}

public static State currentState = State.A;
static void Main(string[] args)
{
var machine = new StateMachine<State, Trigger>(() =>
GetState(), s => SetState(s));
machine.Configure(State.A).Permit(Trigger.AtoB, State.B);
machine.Configure(State.B).Permit(Trigger.BtoC, State.C);
machine.Fire(Trigger.AtoB);

Console.ReadLine();
}

private static void SetState(State state)
{
Console.WriteLine("SET state");
currentState = state;
}

private static State GetState()
{
Console.WriteLine("Get state");
return currentState;
}
}

Grant BlahaErath

unread,
Jan 10, 2012, 1:45:19 AM1/10/12
to dotnet-state-ma...@googlegroups.com
I didn't see an answer for this yet...

While I haven't looked at the source, GetState/SetState behave as if
they are property accessors. They are just hooks so the class user can
implement whatever caching and serialization behavior they want.

You might consider making "UpdateTheServer" one of the states the
machine goes through. State transitions in stateless are about as
lightweight as you can get, so tiny atomic states are pretty useful.

Nicholas Blumhardt

unread,
Jan 10, 2012, 9:33:09 PM1/10/12
to dotnet-state-ma...@googlegroups.com, dotnet-state-ma...@googlegroups.com
Property accessors are a good analogy. The basic usage pattern is something like:

- load current state into a field
- hook state machine up to field with get/set
- fire one or more triggers
- save value of field to database

Hope his helps!
Nick

Reply all
Reply to author
Forward
0 new messages