Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
 public static RuleResult AddPropertyError(this RuleResult rr, string propertyName, string message)
 {
     rr.AddPropertyErrorMessage(propertyName, message);
     return(rr);
 }