Esempio n. 1
0
 /// <summary>
 /// Execute the command with the given typed parameter asynchronously
 /// </summary>
 /// <param name="param">Typed command parameter</param>
 public async Task ExecuteAsync(T param)
 {
     Error = null;
     if (CanExecute(param))
     {
         CommandExecutionState state = CommandExecutionState.Failure;
         CancelEventArgs       args  = new CancelEventArgs(false);
         RaiseExecuting(args);
         if (!args.Cancel)
         {
             try
             {
                 if (ExecuteInBackgroundThread)
                 {
                     await Task.Run(async() => await RunAsync(param));
                 }
                 else
                 {
                     await RunAsync(param);
                 }
                 state = CommandExecutionState.Success;
             }
             catch (Exception ex)
             {
                 Error = ex;
                 state = CommandExecutionState.Failure;
             }
         }
         else
         {
             state = CommandExecutionState.Cancelled;
         }
         RaiseExecuted(state);
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Execute the command with the given typed parameter
 /// </summary>
 /// <param name="param">Typed command parameter</param>
 public override void Execute(T param)
 {
     if (CanExecute(param))
     {
         CommandExecutionState state = CommandExecutionState.Failure;
         CancelEventArgs       args  = new CancelEventArgs(false);
         RaiseExecuting(args);
         if (!args.Cancel)
         {
             try
             {
                 Run(param);
                 state = CommandExecutionState.Success;
             }
             catch (Exception ex)
             {
                 Error = ex;
                 state = CommandExecutionState.Failure;
             }
         }
         else
         {
             state = CommandExecutionState.Cancelled;
         }
         RaiseExecuted(state);
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandContext"/> class.
        /// </summary>
        /// <param name="theCommand">The command that will be executed.</param>
        /// <param name="state">The current state of the command context</param>
        /// <param name="exception">Any exception which may have been thrown while executing</param>
        public CommandContext(ICommand theCommand, CommandExecutionState state = CommandExecutionState.None, Exception exception = null)
        {
            Contract.Requires <ArgumentNullException>(theCommand != null, "The theCommand cannot be null.");

            _theCommand = theCommand;
            Exception   = exception;
            if (state == CommandExecutionState.Resolved)
            {
                ExecutorResolved = true;
            }
            if (state == CommandExecutionState.Called)
            {
                ExecutorResolved      = true;
                ExecutorHasBeenCalled = true;
            }
        }
        public static async Task <HttpResponseMessage> GetCommandState(
            [HttpTrigger(AuthorizationLevel.Function, "GET", Route = @"CQRS/GetCommandState/{domainName}/{commandName}/{commandIdentifier}")] HttpRequestMessage req,
            string domainName,
            string commandName,
            string commandIdentifier
            )
        {
            #region Tracing telemetry
            Activity.Current.AddTag("Domain", domainName);
            Activity.Current.AddTag("Command", commandName);
            Activity.Current.AddTag("Command Identifier", commandIdentifier);
            #endregion

            #region Validate parameters
            if (string.IsNullOrWhiteSpace(domainName))
            {
                return(req.CreateResponse(System.Net.HttpStatusCode.BadRequest, "Missing Domain in command identifier"));
            }
            if (string.IsNullOrWhiteSpace(commandName))
            {
                return(req.CreateResponse(System.Net.HttpStatusCode.BadRequest, "Missing Command Name in command identifier"));
            }
            if (string.IsNullOrWhiteSpace(commandIdentifier))
            {
                return(req.CreateResponse(System.Net.HttpStatusCode.BadRequest, "Missing Command Identifier in command identifier"));
            }
            #endregion

            Command cmd = new Command(domainName, commandName, commandIdentifier);
            if (cmd != null)
            {
                CommandExecutionState cmdState = await cmd.GetExecutionState();

                if (cmdState != null)
                {
                    // return this
                    return(req.CreateResponse <CommandExecutionState>(System.Net.HttpStatusCode.OK,
                                                                      cmdState,
                                                                      @"application/json"));
                }
            }

            return(req.CreateResponse(System.Net.HttpStatusCode.BadRequest,
                                      "Unable to retrieve command"));
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandContext"/> class.
        /// </summary>
        /// <param name="theCommand">The command that will be executed.</param>
        /// <param name="state">The current state of the command context</param>
        /// <param name="exception">Any exception which may have been thrown while executing</param>
        public CommandContext(ICommand theCommand, CommandExecutionState state = CommandExecutionState.None, Exception exception = null)
        {
            Contract.Requires<ArgumentNullException>(theCommand != null, "The theCommand cannot be null.");

            _theCommand = theCommand;
            Exception = exception;
            if (state == CommandExecutionState.Resolved) ExecutorResolved = true;
            if (state == CommandExecutionState.Called)
            {
                ExecutorResolved = true;
                ExecutorHasBeenCalled = true;
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Method to be called by derived commands to indicate completion
 /// </summary>
 /// <param name="state">State of the command</param>
 protected void RaiseExecuted(CommandExecutionState state)
 {
     IsExecuting = false;
     RaiseCanExecuteChanged();
     Executed?.Invoke(this, new ExecutedEventArgs(state));
 }
Esempio n. 7
0
 /// <summary>
 /// Create a new instance of <see cref="ExecutedEventArgs"/>
 /// </summary>
 /// <param name="state">State of the command execution</param>
 public ExecutedEventArgs(CommandExecutionState state)
 {
     this.state = state;
 }