public override async Task <Result <TResult> > HandleAsync(TCommand command, CancellationToken cancellationToken)
        {
            Assert.IsNotNull(command, nameof(command));
            cancellationToken.ThrowIfCancellationRequested();

            var transaction = await CreateTransactionAsync(cancellationToken);

            var result = Result.Failed <TResult>(500, "Transaction decorator throw an exception.");

            try
            {
                result = await InnerHandler.HandleAsync(command, cancellationToken);
            }
            finally
            {
                if (result.IsFailed == false)
                {
                    await CommitTransactionAsync(transaction, cancellationToken);
                }
                else
                {
                    await RollbackTransactionAsync(transaction, cancellationToken);
                }
            }

            return(result);
        }
Example #2
0
        public override Task HandleAsync(Order request, Context context, CancellationToken token)
        {
            if (string.IsNullOrEmpty(request.Address))
            {
                context.SetError(OrderError.Validation);

                return(Task.CompletedTask);
            }

            return(InnerHandler.HandleAsync(request, context, token));
        }
Example #3
0
        public override Task HandleAsync(TestRequestA request, Context context, CancellationToken token = default(CancellationToken))
        {
            _request   = request;
            _timestamp = DateTime.UtcNow;

            if (InnerHandler != null)
            {
                return(InnerHandler.HandleAsync(new TestRequestB(), new Context(), token));
            }
            context.SetResponse(new TestResponse());
            return(Task.CompletedTask);
        }
        public override async Task <Result <TResult> > HandleAsync(TCommand command, CancellationToken cancellationToken)
        {
            try
            {
                return(await InnerHandler.HandleAsync(command, cancellationToken));
            }
            catch (Exception e)
            {
                var commandName = typeof(TCommand).Name;
                var result      = Result.Failed <TResult>((int)HttpStatusCode.InternalServerError, "Umm...");

                _logger.LogError(e, "Action='{CommandName}' TrackId='{TrackId}' Message='Command  handler throw an exception.'", commandName, result.TraceId);

                return(result);
            }
        }
Example #5
0
        public override async Task <Result <TResult> > HandleAsync(TQuery query, CancellationToken cancellationToken = default)
        {
            Assert.IsNotNull(query, nameof(query));
            cancellationToken.ThrowIfCancellationRequested();

            if (_memoryCache.TryGetValue <TResult>(typeof(TQuery).Name, out var value))
            {
                return(Result.Ok(value));
            }

            _logger.LogInformation("Action='{Query}' Message='Cache miss.'", typeof(TQuery).Name);

            var queryResult = await InnerHandler.HandleAsync(query, cancellationToken);

            if (!queryResult.IsFailed)
            {
                _memoryCache.Set(typeof(TQuery).Name, queryResult.Value);
            }

            return(queryResult);
        }
        public override async Task <Result <TResult> > HandleAsync(TCommand command, CancellationToken cancellationToken)
        {
            if (!_logger.IsEnabled(LogLevel.Information))
            {
                return(await InnerHandler.HandleAsync(command, cancellationToken));
            }

            var commandName = typeof(TCommand).Name;
            var timer       = Stopwatch.StartNew();

            _logger.LogInformation("Action='{Command}' Message='Command handler started.'", commandName);

            try
            {
                return(await InnerHandler.HandleAsync(command, cancellationToken));
            }
            finally
            {
                timer.Stop();
                _logger.LogInformation("Action='{Command}' ElapsedMilliseconds='{ElapsedMilliseconds}' Message='Command handler executed.'", commandName, timer.ElapsedMilliseconds);
            }
        }
Example #7
0
 public Task HandleAsync(string input, ILambdaContext context)
 {
     return(InnerHandler.HandleAsync(input, context));
 }