Пример #1
0
        private async Task <bool> ExecuteRuleAsync(ValidationRule rule, ISet <object> failedTargets, ValidationResult validationResultAccumulator, SynchronizationContext syncContext)
        {
            // Skip rule if the target is already invalid and the rule is not configured to execute anyway
            if (failedTargets.Contains(rule.Target) && !ShouldExecuteOnAlreadyInvalidTarget(rule))
            {
                // Assume that the rule is valid at this point because we are not interested in this error until
                // previous rule is fixed.
                SaveRuleValidationResultAndNotifyIfNeeded(rule, RuleResult.Valid(), syncContext);

                return(true);
            }

            var ruleResult = !rule.SupportsSyncValidation
                ? await rule.EvaluateAsync().ConfigureAwait(false)
                : rule.Evaluate();

            SaveRuleValidationResultAndNotifyIfNeeded(rule, ruleResult, syncContext);

            AddErrorsFromRuleResult(validationResultAccumulator, rule, ruleResult);

            if (!ruleResult.IsValid)
            {
                failedTargets.Add(rule.Target);
            }

            return(true);
        }
        private RuleResult ExecuteRuleCore(ValidationRule rule)
        {
            RuleResult result = RuleResult.Valid();

            if (!rule.SupportsSyncValidation)
            {
                var completedEvent = new ManualResetEvent(false);

                rule.EvaluateAsync().ContinueWith(t =>
                {
                    result = t.Result;
                    completedEvent.Set();
                });

                var isCompleted = completedEvent.WaitOne(AsyncRuleExecutionTimeout);

                if (!isCompleted)
                {
                    throw new TimeoutException("Rule validation did not complete in specified timeout.");
                }
            }
            else
            {
                result = rule.Evaluate();
            }

            return(result);
        }
Пример #3
0
        private static Task <RuleResult> ExecuteRuleCoreAsync(ValidationRule rule)
        {
            Task <RuleResult> resultTask;

            if (!rule.SupportsSyncValidation)
            {
                resultTask = rule.EvaluateAsync();
            }
            else
            {
                var tcs = new TaskCompletionSource <RuleResult>();

                var result = rule.Evaluate();

                tcs.SetResult(result);

                resultTask = tcs.Task;
            }

            return(resultTask);
        }