Example #1
0
        public static CommandHandlerResult OkDelayed <T>(T owner, Func <T, object> delayed)
        {
            var result = new CommandHandlerResult(HttpStatusCode.OK, null);

            result._delayedContent = delayed;

            return(result);
        }
        private CommandResult GetCommandResultFromCommandHandlerResult(CommandHandlerResult result)
        {
            if (result == CommandHandlerResult.Ok)
            {
                return(CommandResult.Ok);
            }

            var content = result.Content;

            return(new CommandResult(result.StatusCode, content));
        }
        public async Task <CommandResult> SendAsync <TCommand>(TCommand command) where TCommand : ICommand
        {
            try
            {
                var innerHandler = (IHandleCommand <TCommand>)
                                   _scope.ResolveOptional(typeof(IHandleCommand <TCommand>));

                if (innerHandler != null)
                {
                    command.Validate();

                    if (command.IsValid)
                    {
                        _innerOkResult = await innerHandler.HandleAsync(command);

                        if (_innerOkResult == null || !_innerOkResult.HasDelayedContent)
                        {
                            return(GetCommandResultFromCommandHandlerResult(_innerOkResult));
                        }

                        _innerOkResult.ResolveAndUpdateDelayedContent();

                        return(GetCommandResultFromCommandHandlerResult(_innerOkResult));
                    }
                    else
                    {
                        return(CommandResult.FromValidationErrors(command.ValidationErrorMessages));
                    }
                }

                return(CommandResult.NonExistentCommand(typeof(TCommand).Name));
            }
            catch (SecurityException ex)
            {
                return(new CommandResult(ex, HttpStatusCode.Forbidden));
            }
            catch (Exception ex)
            {
                return(new CommandResult(ex, HttpStatusCode.InternalServerError));
            }
        }