Ejemplo n.º 1
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            try
            {
                // Pre Validation
                await PreValidation(request);

                // Post Validation

                // Create data layer transaction
                await using var transaction = await _dataLayer.Database.BeginTransactionAsync(cancellationToken);

                // Pre Handler
                _response = await next();

                // Post Handler

                // Commit data layer transaction
                await transaction.CommitAsync(cancellationToken);
                await PostHandler(request);

                return(_response);
            }
            catch (Exception e)
            {
                _dataLayer.RollBack();
                var responseInstance = Activator.CreateInstance(next.GetType().GenericTypeArguments[0]);

                responseInstance?.GetType().GetProperty("Message")?
                .SetValue(responseInstance, $"Error: {e.Message};", null);

                if (e.InnerException != null)
                {
                    responseInstance?.GetType().GetProperty("Message")?
                    .SetValue(responseInstance, $"{responseInstance?.GetType().GetProperty("Message")?.GetValue(responseInstance)} Inner Exception: {e.InnerException?.Message}", null);
                }

                responseInstance?.GetType().GetProperty("HttpStatusCode")?
                .SetValue(responseInstance, HttpStatusCode.InternalServerError, null);

                return((TResponse)responseInstance);
            }
        }
Ejemplo n.º 2
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            try
            {
                // Pre Validation
                await PreValidation(request);

                // Post Validation

                // Pre Handler
                _response = await next();

                // Post Handler
                await PostHandler(request);

                return(_response);
            }
            catch (Exception e)
            {
                var responseInstance = Activator.CreateInstance(next.GetType().GenericTypeArguments[0]);

                responseInstance?.GetType().GetProperty("Message")?
                .SetValue(responseInstance, $"Error: {e.Message};", null);

                if (e.InnerException != null)
                {
                    responseInstance?.GetType().GetProperty("Message")?
                    .SetValue(responseInstance, $"{responseInstance?.GetType().GetProperty("Message")?.GetValue(responseInstance)} Inner Exception: {e.InnerException?.Message}", null);
                }

                responseInstance?.GetType().GetProperty("HttpStatusCode")?
                .SetValue(responseInstance, HttpStatusCode.InternalServerError, null);

                return((TResponse)responseInstance);
            }
        }
Ejemplo n.º 3
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            Log.Debug($"-- Handling request: {request.GetType()} | Awaiting response: {next.GetType()}");
            var response = await next();

            Log.Debug($"-- Finished request: {request.GetType()}");
            return(response);
        }