Fork me on GitHub

Persistence

The persistence mechanism can be used to save and later reload the current state and the history states (which state was last active in a super state) of the state machine.

Saving a state machine:

Loading a state machine:

Implementing the saver and loader

The saver needs to implement the interface IStateMachineSaver<TState>. The state machine will then call your saver and pass it the current state and a dictionary containing the history states (key = id of super state, value = id of this super state's last active state). If there is no current state (the state machine is not started yet), no call to save the current state will be made. Also, only super states with a last active state are contained in the dictionary. Super states that were never visited and therefore have no last active state are omitted.

The saver gets the current state in an Initializable. Only if the state machine was once started, the initializable will contain the current state; otherwise it will be not initialized.

Only super states with a last active state are contained in the dictionary. Super states that were never visited and therefore have no last active state are omitted.

A saver looks like this:

The loader needs to implement the interface IStateMachineLoader<TState>. The state machine will then call your loader to get the current state and the history states. You should return exactly the same values, which were passed to the saver (the same values, not necessarily the same objects, of course).

A loader looks like this:

Currently, there are no default savers and loaders available. If you write one, please contribute it :-)