2

I have a state machine as shown below. I have implemented this in C# using Stateless.

The On state can reenter itself in case of the RequestStatus trigger. When this happens, the statemachine can be in any of the following states: On, On > Standby or On > Operational. Now, for this trigger, the current state should remain as it is and just the status should be returned i.e., execute ReturnStatus().

Executing functions depending on the trigger can be done using the OnEntryFrom(Trigger, Action) method. But the problem is, if I set OnEntryFrom of On state, every time the trigger is triggered, if the either of the substates are active, they are first exited, then the On state is exited and then the OnEntry of On state occurs.

Now what I want is, regardless of which of the 3 states the statemachine is active in, for the RequestStatus trigger, just execute the ReturnStatus() method.

One way I could do this is set OnEntryFrom(PowerOn, () => Initialize()) and OnEntryFrom(RequestStatus, () => ReturnStatus()). Do the same for the substates by adding new triggers StandbyRequestStatus and OperationalRequestStatus and trigger them according to which one was previously active. Which I think is tedious, especially if the number of substates grows.

The other way is to retain the substate and handle it just once. But I can't find a way to do this in Stateless.

Is it achievable using Stateless? Has anyone done this before?

Stateless Statemachine Used

Xpleria
  • 5,472
  • 5
  • 52
  • 66

0 Answers0