Ejemplo n.º 1
0
        /// <summary>
        /// Executes the registered command by invoking the contained action.
        /// </summary>
        /// <param name="result">The result of the command parsing process pertinent to the registered command.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given command result is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown when invocation of the contained action fails.</exception>
        public void Execute(CommandResult result)
        {
            VerificationProvider.VerifyNotNull(result, "result");

            // Get hold of the relevant converter for the command value type
            var    converter = mObjectConverterFactory.Create <TValue>();
            TValue value     = converter.Convert(result.Value);

            try
            {
                mExecutionAction.Invoke(value);
            }
            catch (Exception ex)
            {
                // Wrap the exception within an InvalidOperationException and re-throw
                throw new InvalidOperationException(
                          "An error occurred while executing the command. See inner exception for details.", ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to retrieve a value from the set of commands that is stored under the given key.
        /// </summary>
        /// <typeparam name="TValue">The type of the value to be retrieved.</typeparam>
        /// <param name="key">The key of the command to retrieve.</param>
        /// <param name="value">An output parameter that will be populated with the requested value.</param>
        /// <returns>True if the given key matches a stored command; false otherwise.</returns>
        /// <exception cref="ArgumentException">Thrown when the given key value is not a valid string.</exception>
        public bool TryGetValue <TValue>(string key, out TValue value)
        {
            VerificationProvider.VerifyValidString(key, "key");

            // Initialize the output parameter with a default value
            value = default(TValue);

            CommandResult command;

            if (!mKeyedCommands.TryGetValue(key, out command))
            {
                // The command does not exist under the given key
                return(false);
            }

            // We have the command, so get hold of the relevant converter for its type
            var converter = mObjectConverterFactory.Create <TValue>();

            value = converter.Convert(command.Value);

            return(true);
        }