示例#1
0
        public static async Task <TResult> ExecuteAsync <TRequest, TResult>(this ControllerBase that, IAsyncAspCommand <TRequest, TResult> command, TRequest request)
        {
            if (that is ControllerBase)
            {
                command.SetController((ControllerBase)that);
            }

            if (CommandRunningConfiguration.Handlers.Length == 0)
            {
                return(await command.ExecuteExternalAsync(request));
            }

            return(await ExecuteThroughHandlers(command, request));
        }
示例#2
0
        private static async Task <TResult> ExecuteThroughHandlers <TRequest, TResult>(IAsyncAspCommand <TRequest, TResult> command, TRequest request)
        {
            var result       = default(TResult);
            var handlerIndex = 0;
            var commandType  = command.GetType();

            var handlers = RetrieveOrderedHandlers();

            await RunHandlersBeforeMethods <IAsyncAspCommand <TRequest, TResult>, TRequest, TResult>(
                handlers, commandType, request, handlerIndex);

            try
            {
                result = await command.ExecuteExternalAsync(request);
            }
            catch (Exception e)
            {
                var message = $"Failed to run  Execute on {handlers[handlerIndex].GetType()}";
                await RunHandlersAfterMethods
                    (handlers, commandType, result, handlerIndex, message);

                throw new Exception(message, e);
            }
            finally
            {
                await RunHandlersAfterMethods
                    (handlers, commandType, result, handlerIndex);
            }

            return(result);
        }
示例#3
0
 public static async Task <TInjectedResult> ExecuteAsync <TRequest, TResult, TInjectedRequest, TInjectedResult>
     (this IAsyncAspCommand <TRequest, TResult> extendedCommand, IAsyncAspCommand <TInjectedRequest, TInjectedResult> command, TInjectedRequest request)
 {
     return(await command.ExecuteExternalAsync(request));
 }