Ejemplo n.º 1
0
        public MessageEventResult ProcessMessage(MessageEvent messageEvent)
        {
            IStateFactory stateFactory = StateFactory;

            if (!_stateStorage.TryGetValue(messageEvent.Id, out IState currentState))
            {
                currentState = stateFactory.GetInitialState();
                SetState(messageEvent.Id, currentState);
            }
            MessageEventResult result;

            try
            {
                result = currentState.HandleEvent(messageEvent);
                if (!FinalStateReached(messageEvent.Id))
                {
                    IState nextState = stateFactory.GetNextState(currentState);
                    SetState(messageEvent.Id, nextState);
                }
                else
                {
                    SetState(messageEvent.Id, stateFactory.GetInitialState());
                }
            }
            catch (PreviousStateUserAnswerException prevStateAnswerEx)
            {
                result = new MessageEventResult(prevStateAnswerEx.Message);
            }
            return(result);
        }