public void AddRule(IValidationRule rule)
 {
     foreach (var r in _rules)
     {
         if (r.GetType() == rule.GetType())
             return;
     }
     _rules.Add(rule);
 }
Example #2
0
        private void AppendValidationRuleOutput(IValidationRule validationRule, StringBuilder sb, ILogContext logContext)
        {
            sb.AppendLine().AppendLine();
            sb.Append(new string (' ', 4) + "-> ");
            sb.Append(GetTypeName(validationRule.GetType()));

            AppendGroupedValidatorsOutput(validationRule.Validators.ToArray(), "VALIDATORS:", sb);
            AppendMergeOutput(logContext.GetLogContextInfos(validationRule).ToArray(), sb);
        }
Example #3
0
 public void AddRule(IValidationRule rule)
 {
     foreach (var r in _rules)
     {
         if (r.GetType() == rule.GetType())
         {
             return;
         }
     }
     _rules.Add(rule);
 }
Example #4
0
        internal Rule(IValidationRule rule)
        {
            _validationRule = rule;

            if (rule is HandlerRule)
            {
                this.Key = (rule as HandlerRule).GetKeyString();
            }
            else
            {
                this.Key = rule.GetType().FullName;
            }
        }
Example #5
0
        internal Rule(IValidationRule rule)
        {
            _validationRule = rule;

            if (rule is HandlerRule)
            {
                this.Key = (rule as HandlerRule).GetKeyString();
            }
            else
            {
                this.Key = rule.GetType().FullName;
            }
        }
Example #6
0
        private async Task <Result <TData> > ValidateAsync <TData>(IValidationRule validationRule)
        {
            var validationRuleType        = validationRule.GetType();
            var validationRuleHandlerType = typeof(IValidationRuleHandler <>).MakeGenericType(validationRuleType);
            var methodInfo = validationRuleHandlerType.GetMethod(nameof(IValidationRuleHandler <IValidationRule> .ExecuteAsync),
                                                                 BindingFlags.Public | BindingFlags.Instance);

            Check.NotNull(methodInfo, nameof(IValidationRuleHandler <IValidationRule> .ExecuteAsync));

            var validationRuleHandler = _serviceProvider.GetService(validationRuleHandlerType);

            Check.NotNull(validationRuleHandler, $"handler not found in DI container by type {validationRuleHandlerType.FullName}");

            var result = await(Task <ValidationResult>) methodInfo.Invoke(validationRuleHandler, new object[] { validationRule });

            return(result as Result <TData>);
        }
        private async ValueTask <RuleResult[]> HandleAsync(IValidationRule <T> rule, T[] items, CancellationToken cancellationToken)
        {
            // Creates final handler that executes the rule itself.
            async ValueTask <ValidationResult[]> finalHandler()
            {
                return(await rule.ValidateAsync(items, cancellationToken));
            }

            // Creates the handler chain by reversing the original list of handlers then aggregating them with the final handler as the seed.
            var handlerChain = _handlers
                               .Reverse()
                               .Aggregate((ValidationRuleHandlerDelegate)finalHandler,
                                          (next, handler) => () => handler.HandleAsync(rule.GetType(), items, next, cancellationToken));

            var validationResult = await handlerChain();

            return(validationResult
                   .Select(result => new RuleResult(rule.GetType(), result))
                   .ToArray());
        }
        public IValidationRule <TViewModel> ConvertValidationRuleModelTo <TViewModel, TOtherViewModel>(IValidationRule <TOtherViewModel> rule) where TViewModel : class where TOtherViewModel : class
        {
            if (typeof(TViewModel) == typeof(TOtherViewModel))
            {
                return(rule as IValidationRule <TViewModel>);
            }

            Type genericType = rule.GetType();

            if (genericType.IsGenericType)
            {
                genericType = genericType.GetGenericTypeDefinition();
                genericType = genericType.MakeGenericType(new[] { typeof(TViewModel) });
            }

            IEnumerable <object> convertedExpressions = rule.ConstructorArguments.Select(o => ConvertExpressionTo <TViewModel>(o)).ToList();

            var types = convertedExpressions.Select(o => o.GetType()).ToArray();

            var constructor = genericType.GetConstructor(types);

            return((IValidationRule <TViewModel>)constructor.Invoke(convertedExpressions.ToArray()));
        }
Example #9
0
        protected virtual void AddRule(IValidationRule <TEntity> validationRule)
        {
            var ruleName = validationRule.GetType() + Guid.NewGuid().ToString("D");

            _validationsRules.Add(ruleName, validationRule);
        }
Example #10
0
 public ValidatorModel(IValidationRule rule)
 {
     FullName    = rule.GetType().FullName;
     Description = rule.Description;
     Active      = true;
 }