Example #1
0
        /// <summary>
        /// Submits the specified command for execution.
        /// </summary>
        /// <typeparam name="TResponseData">The type of the response data.</typeparam>
        /// <param name="command">The command.</param>
        /// <returns>The command response.</returns>
        public CommandHandlerResponse <TResponseData> Submit <TResponseData>(ICommand <TResponseData> command)
        {
            var response = new CommandHandlerResponse <TResponseData>();

            Guard.Will.ProtectAgainstNullArgument(() => command);

            this.OnCommandExecuting(new CommandExecutionStatusChangedEventArgs(command, CommandExecutionStatus.Executing));

            var commandType = command.GetType();

            try
            {
                if (command is IValidatableObject)
                {
                    var validationResults = Validate((IValidatableObject)command);

                    if (validationResults.Any())
                    {
                        response.ValidationResults = validationResults;

                        response.CommandExecutionResult = CommandExecutionResult.Failed;

                        this.OnCommandExecuting(new CommandExecutionStatusChangedEventArgs(command, CommandExecutionStatus.Failing));
                    }
                    else
                    {
                        ExecuteCommand(command, commandType, response);
                    }
                }
                else
                {
                    ExecuteCommand(command, commandType, response);
                }
            }
            catch (KeyNotFoundException)
            {
                // This indicates we couldn't find a handler so we gotta bail...
                this.OnCommandExecuting(new CommandExecutionStatusChangedEventArgs(command, CommandExecutionStatus.Failing));

                response.Exception = new CommandHandlerNotFoundException();

                response.CommandExecutionResult = CommandExecutionResult.Failed;

                if (ThrowExceptionOnFailure)
                {
                    throw response.Exception;
                }
            }
            catch (Exception ex)
            {
                this.OnCommandExecuting(new CommandExecutionStatusChangedEventArgs(command, CommandExecutionStatus.Failing));

                response.Exception = ex;

                response.CommandExecutionResult = CommandExecutionResult.Failed;

                if (ThrowExceptionOnFailure)
                {
                    throw;
                }
            }

            this.OnCommandExecuting(new CommandExecutionStatusChangedEventArgs(command, CommandExecutionStatus.Finished));

            return(response);
        }
Example #2
0
        private void ExecuteCommand <TResponseData>(ICommand <TResponseData> command, Type commandType, CommandHandlerResponse <TResponseData> response)
        {
            var func = createCommandDelegates.GetOrAdd(Tuple.Create(commandType, typeof(TResponseData)), GenerateCommandDelegate);

            var watch = System.Diagnostics.Stopwatch.StartNew();

            response.Data = (TResponseData)func(this, command);

            watch.Stop();

            response.ExecutionTimeInMilliseconds = watch.ElapsedMilliseconds;

            response.CommandExecutionResult = CommandExecutionResult.Succeeded;
        }