public bool Load(IStateMachineLoader <TState> stateMachineLoader) { Guard.AgainstNullArgument(nameof(stateMachineLoader), stateMachineLoader); this.CheckThatStateMachineIsNotAlreadyInitialized(); Initializable <TState> loadedCurrentState = stateMachineLoader.LoadCurrentState(); IDictionary <TState, TState> historyStates = stateMachineLoader.LoadHistoryStates(); var initialized = SetCurrentState(); LoadHistoryStates(); NotifyExtensions(); return(initialized); bool SetCurrentState() { if (loadedCurrentState.IsInitialized) { this.currentState = this.states[loadedCurrentState.Value]; return(true); } this.currentState = null; return(false); } void LoadHistoryStates() { foreach (KeyValuePair <TState, TState> historyState in historyStates) { IState <TState, TEvent> superState = this.states[historyState.Key]; IState <TState, TEvent> lastActiveState = this.states[historyState.Value]; if (!superState.SubStates.Contains(lastActiveState)) { throw new InvalidOperationException(ExceptionMessages.CannotSetALastActiveStateThatIsNotASubState); } superState.LastActiveState = lastActiveState; } } void NotifyExtensions() { this.extensions.ForEach( extension => extension.Loaded( this, loadedCurrentState, historyStates)); } }
private void LoadCurrentState(IStateMachineLoader <TState> stateMachineLoader) { Initializable <TState> loadedCurrentState = stateMachineLoader.LoadCurrentState(); this.currentState = loadedCurrentState.IsInitialized ? this.states[loadedCurrentState.Value] : null; }