I am investigating if using a state machine, like Stateless, can help improve our code. I am just starting to learn about it, so I apologize in advance if I should have been able to easily figure this out on my own.
As I have looked at the examples, the code is more or less like the following:
public MyThing()
{
// Instantiate a new state machine in the Open state
_machine = new StateMachine<State, Trigger>(State.Open);
// Configure the Open state
_machine.Configure(State.Open)
.Permit(Trigger.Assign, State.Assigned);
// Configure the Deferred state
_machine.Configure(State.Deferred)
.OnEntry(() => _assignee = null)
.Permit(Trigger.Assign, State.Assigned);
}
The key part is that the configuring of the state machine is happening in the constructor and is happening then each time the object is instantiated. In my case, there are many times when the state machine would never come into play, for example, if I am simply returning a list of Things.
It seems odd to me that the configuration of the state machine is happening each time Thing is constructed. As such, I have concerns about this from a performance perspective, which leads to the following questions:
Is the cost of configuring the state machine negligible, so that there is no real concern about configuring the state machine, even when the state machine is not necessary?
Assuming the cost is not negligible, is there a way the state machine definition can be "reused". That is, the state machine for Thing is defined during application initialization, and then when instantiating Thing, the "global" state machine definition can be used?
Any help is greatly appreciated.
Thanks, Eric