Search code examples
c#.netstate-machinestateless-state-machine

Should I fire trigger to change state from the OnEntry() method in finite state machine?


I am using stateless framework (https://code.google.com/p/stateless/) to model finite state machine in my application. There are certain states that should perform some logic and then immediately move to the next state. I am wondering is it good practice to do this like following:

            var machine = new StateMachine<State, Trigger>(State.Idle);
            machine.Configure(State.StateA)
            .OnEntry(() =>
            {
                DoSomeStuff();
               _machine.Fire(Trigger.TriggerB);     // move to StateB
            });

Is this good FSM design? If not, what would be better approach? The idea I am trying to implement is to have certain states that automatically advance machine to the next state without having some external code that waits for DoSomeStuff() to finish and then to trigger the machine to move in the next state.


Solution

  • You seem to be talking about state push vs state pull. Both works, one approach can be more efficient in some situations.

    It's perfectly fine to have state push approach, where one state does some work and calls a transition.