Пример #1
0
        /// <summary>
        ///   Adds a rule to the validation plan.
        /// </summary>
        /// <param name="rule"> The rule to add. </param>
        /// <param name="setup"> A function that further sets up the added rule. </param>
        /// <returns> </returns>
        public ValidationPlan <TTarget> AddRule(
            Func <TTarget, bool> rule,
            Func <ValidationRule <TTarget>, ValidationRule <TTarget> > setup = null)
        {
            var validationRule = new ValidationRule <TTarget>(rule);

            if (setup != null)
            {
                validationRule = setup(validationRule);
            }
            rules.Add(validationRule);
            return(this);
        }
Пример #2
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="ValidationRule{TTarget}" /> class.
        /// </summary>
        /// <param name="fromRule"> The rule upon which the new rule wil be based. </param>
        /// <remarks>
        ///   This is used to clone a rule.
        /// </remarks>
        protected ValidationRule(ValidationRule <TTarget> fromRule)
        {
            condition = fromRule.condition;
            CreatesEvaluationsAsInternal = fromRule.CreatesEvaluationsAsInternal;

            if (fromRule.preconditions != null)
            {
                preconditions = new List <IValidationRule <TTarget> >(fromRule.preconditions);
            }

            if (fromRule.extensions != null)
            {
                extensions = new Dictionary <Type, object>(fromRule.extensions);
            }

            if (fromRule.MessageGenerator != null)
            {
                MessageGenerator = fromRule.MessageGenerator;
            }
        }
Пример #3
0
        private static IEnumerable <IValidationRule <TTarget> > AllPreconditions(ValidationRule <TTarget> rule)
        {
            if (rule?.preconditions != null)
            {
                foreach (var precondition in rule.preconditions)
                {
                    // if the precondition is a ValidationPlan, yield all of its rules and the plan itself
                    // this will traverse preconditions
                    var preconditionPlan = precondition as ValidationPlan <TTarget>;
                    if (preconditionPlan != null)
                    {
                        foreach (var planRule in preconditionPlan.AllRules())
                        {
                            yield return(planRule);
                        }

                        yield return(preconditionPlan);

                        continue;
                    }

                    // otherwise, recursively check the rule's preconditions, so that the last will be yielded first
                    var preconditionRule = precondition as ValidationRule <TTarget>;
                    if (preconditionRule != null)
                    {
                        foreach (var prePreCondition in AllPreconditions(preconditionRule))
                        {
                            yield return(prePreCondition);
                        }
                    }

                    // if the precondition is not a ValidationPlan, yield just the rule
                    yield return(precondition);
                }
            }
        }