Ejemplo n.º 1
0
        public async Task <RuleCheckResult> CheckAsync(TTarget target)
        {
            var unconformities       = new Dictionary <string, string>();
            var unexpectedExceptions = new List <Exception>();

            foreach (var rule in Rules)
            {
                RuleCheckResult result;

                try
                {
                    result = await rule.CheckAsync(target);
                }
                catch (Exception ex)
                {
                    result = new RuleCheckResult(ex);
                }

                if (!result.IsSuccess)
                {
                    if (result.UnexpectedException != null)
                    {
                        unexpectedExceptions.Add(result.UnexpectedException);
                    }

                    if (result.Unconformities != null && result.Unconformities.Any())
                    {
                        result.Unconformities.ToList().ForEach(u =>
                        {
                            unconformities.Add(
                                rule.Code.Equals(u.Key)
                                ? rule.Code
                                : $"{rule.Code}:{u.Key}", u.Value);
                        });
                    }
                    else
                    {
                        unconformities.Add(rule.Code, rule.Description);
                    }
                }
            }

            if (!unconformities.Any() && !unexpectedExceptions.Any())
            {
                return(Success);
            }

            if (unexpectedExceptions.Any())
            {
                return(new RuleCheckResult(new AggregateException(unexpectedExceptions), unconformities));
            }
            else
            {
                return(new RuleCheckResult(false, unconformities));
            }
        }
Ejemplo n.º 2
0
        public async Task EnsureAsync(TTarget target, string exceptionMessageTemplate = null)
        {
            RuleCheckResult result = null;

            try
            {
                result = await CheckAsync(target);
            }
            catch (Exception ex)
            {
                result = new RuleCheckResult(ex);
            }

            if (result.IsSuccess)
            {
                return;
            }

            throw !string.IsNullOrWhiteSpace(exceptionMessageTemplate)
                ? new ViolatedRuleException(this, result.Unconformities, exceptionMessageTemplate)
                : new ViolatedRuleException(this, result.Unconformities);
        }
Ejemplo n.º 3
0
        private async Task EnsureRuleAsync(IRuleFor <TTarget> rule, TTarget target,
                                           string exceptionMessageTemplate = null)
        {
            RuleCheckResult result;

            try
            {
                result = await rule.CheckAsync(target);
            }
            catch (Exception ex)
            {
                result = new RuleCheckResult(ex);
            }

            if (result.IsSuccess)
            {
                return;
            }

            throw !string.IsNullOrWhiteSpace(exceptionMessageTemplate)
                ? new ViolatedRuleException(rule, result.Unconformities, exceptionMessageTemplate)
                : new ViolatedRuleException(rule, result.Unconformities);
        }