예제 #1
0
        public void MarkInvalid(string message)
        {
            if (cancellationTokenSource != null)
            {
                cancellationTokenSource.Cancel();
            }

            Results.OverrideResult = RuleResult.PropertyError(typeof(T).FullName, message);
        }
예제 #2
0
        private async Task RunRule(IRule r, CancellationToken token)
        {
            IRuleResult result = null;

            try
            {
                if (r is IRule <T> rule)
                {
                    result = await rule.Execute(Target, token);
                }
                else
                {
                    throw new InvalidRuleTypeException($"{r.GetType().FullName} cannot be executed for {typeof(T).FullName}");
                }

                if (token.IsCancellationRequested)
                {
                    return;
                }
            }

            catch (Exception ex)
            {
                // If there is an error mark all properties as failed
                foreach (var p in r.TriggerProperties)
                {
                    result = RuleResult.PropertyError(p, ex.Message);
                }
            }

            if (result.IsError)
            {
                result.TriggerProperties    = r.TriggerProperties;
                Results[(int)r.UniqueIndex] = result;
            }
            else if (Results.ContainsKey((int)r.UniqueIndex))
            {
                // Optimized approach for when/if this is serialized
                Results.Remove((int)r.UniqueIndex);
            }
        }