public void Intercept(IInvocation invocation)
        {
            MethodInfo method;

            try
            {
                method = invocation.MethodInvocationTarget;
            }
            catch
            {
                method = invocation.GetConcreteMethod();
            }

            var failures = _validator.Validate(method, invocation.Arguments);
            var result   = failures.ToResult();

            if (!result.Failed)
            {
                invocation.Proceed();
                return;
            }

            if (invocation.Method.IsAsync())
            {
                InterceptAsync(invocation, result);
            }
            else
            {
                InterceptSync(invocation, result);
            }
        }
        public void Intercept(IInvocation invocation)
        {
            MethodInfo method;

            try
            {
                method = invocation.MethodInvocationTarget;
            }
            catch
            {
                method = invocation.GetConcreteMethod();
            }

            if (method.ValidationIgnored())
            {
                invocation.Proceed();
                return;
            }

            _logger.LogInformation(
                "Starting Validation: {TypeName}.{MethodName}", invocation.TargetType?.FullName, method.Name);

            var failures = _validator.Validate(method, invocation.Arguments);

            var failureList = failures.ToList();
            var result      = failureList.Count > 0 ? Result.Fail(null, failureList) : Result.Ok();


            if (!result.Failed)
            {
                _logger.LogInformation(
                    "Validation Completed Successfully: {TypeName}.{MethodName}", invocation.TargetType?.FullName,
                    method.Name);

                invocation.Proceed();
                return;
            }

            _logger.LogInformation(
                "Validation Failed: {TypeName}.{MethodName} {@Failures}", invocation.TargetType?.FullName, method.Name,
                result.Failures);

            if (invocation.Method.IsAsync())
            {
                InterceptAsync(invocation, result);
            }
            else
            {
                InterceptSync(invocation, result);
            }
        }
Esempio n. 3
0
        public void Intercept(IInvocation invocation)
        {
            MethodInfo method;

            try
            {
                method = invocation.MethodInvocationTarget;
            }
            catch
            {
                method = invocation.GetConcreteMethod();
            }

            if (method.ValidationIgnored())
            {
                invocation.Proceed();
                return;
            }

            _logger.LogInformation(
                $"Starting Validation: {invocation.TargetType?.FullName}.{method.Name}");

            var failures = _validator.Validate(method, invocation.Arguments);
            var result   = failures.ToResult();

            if (!result.Failed)
            {
                _logger.LogInformation(
                    $"Validation Completed Successfully: {invocation.TargetType?.FullName}.{method.Name}");

                invocation.Proceed();
                return;
            }

            _logger.LogInformation(
                $"Validation Failed: {invocation.TargetType?.FullName}.{method.Name} {result.Failures.Select(f => f.ToString()).PackToString(",")}");

            if (invocation.Method.IsAsync())
            {
                InterceptAsync(invocation, result);
            }
            else
            {
                InterceptSync(invocation, result);
            }
        }