private StateDefinition <TState> BuildStateDefinition(
            TState state,
            StateConfiguration stateConfiguration
            )
        {
            var commandHandlers = new ICommandHandler[MessageTypeIdProvider.MaxId];

            foreach (var commandHandler in stateConfiguration.CommandHandlers)
            {
                commandHandlers[commandHandler.Key] = commandHandler.Value;
            }

            var stateTransitions = new StateTransition <TState> [MessageTypeIdProvider.MaxId][];

            foreach (var stateTransition in stateConfiguration.StateTransitions)
            {
                stateTransitions[stateTransition.Key] = stateTransition.Value.ToArray();
            }

            return(new StateDefinition <TState>(
                       state,
                       commandHandlers,
                       stateTransitions,
                       stateConfiguration.EnterAction,
                       stateConfiguration.ExitAction
                       ));
        }
        private void AddState(TState state, StateConfiguration configuration)
        {
            if (!_states.TryGetValue(state, out var existingConfiguration))
            {
                existingConfiguration = new StateConfiguration();
                _states[state]        = existingConfiguration;
            }

            foreach (var messageHandler in configuration.CommandHandlers)
            {
                existingConfiguration.CommandHandlers.Add(messageHandler.Key, messageHandler.Value);
            }

            foreach (var stateTransitions in configuration.StateTransitions)
            {
                if (!existingConfiguration.StateTransitions.TryGetValue(
                        stateTransitions.Key,
                        out var existingTransitions
                        ))
                {
                    existingTransitions = new List <StateTransition <TState> >();
                    existingConfiguration.StateTransitions[stateTransitions.Key] = existingTransitions;
                }

                foreach (var stateTransition in stateTransitions.Value)
                {
                    existingTransitions.Add(stateTransition);
                }
            }

            existingConfiguration.EnterAction = configuration.EnterAction ?? existingConfiguration.EnterAction;
            existingConfiguration.ExitAction  = configuration.ExitAction ?? existingConfiguration.ExitAction;
        }