コード例 #1
0
ファイル: RuleBuilder.cs プロジェクト: ne4ta/SpecExpress
        /// <summary>
        /// Facilitates the grouping of a subset of rules to dictate precidence for And / Or opertations.
        /// </summary>
        /// <example>
        ///    Validate ActiveDate is in a five day window starting 10 days ago OR a five day window starting in 5 days from now.
        ///    spec.Check(c => c.ActiveDate).Required()
        ///        .Group(d => d.GreaterThan(DateTime.Now.AddDays(-10))
        ///                        .And.LessThan(DateTime.Now.AddDays(-5)))
        ///        .Or
        ///        .Group(d => d.GreaterThan(DateTime.Now.AddDays(5))
        ///                        .And.LessThan(DateTime.Now.AddDays(10)));
        /// </example>
        /// <param name="rules"><see cref="Action&lt;RuleBuilder&lt;T, TProperty&gt;&gt;"/></param>
        /// <returns><see cref="IAndOr&lt;T, TProperty&gt;"/></returns>
        public IAndOr <T, TProperty> Group(Action <RuleBuilder <T, TProperty> > rules)
        {
            var innerPropertyValidator = new PropertyValidator <T, TProperty>(_propertyValidator);
            var groupRules             = new RuleBuilder <T, TProperty>(innerPropertyValidator);

            rules(groupRules);
            if (OrNextRule)
            {
                if (NextRuleIsConditional)
                {
                    _propertyValidator.ConditionalOrGroup(innerPropertyValidator);
                }
                else
                {
                    _propertyValidator.OrGroup(innerPropertyValidator);
                }
            }
            else
            {
                if (NextRuleIsConditional)
                {
                    _propertyValidator.ConditionalAndGroup(innerPropertyValidator);
                }
                else
                {
                    _propertyValidator.AndGroup(innerPropertyValidator);
                }
            }

            NextRuleIsConditional = false;
            OrNextRule            = false;

            return(new ActionJoinBuilder <T, TProperty>(_propertyValidator));
        }