Esempio n. 1
0
        /// <summary>
        /// 检查某个属性是否满足规则
        /// </summary>
        /// <param name="target">要验证的实体。</param>
        /// <param name="property">托管属性</param>
        /// <param name="actions">验证时的行为。</param>
        /// <param name="ruleFilter">要验证的规则的过滤器。</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">target
        /// or
        /// property</exception>
        public static BrokenRulesCollection Validate(
            this Entity target, IManagedProperty property,
            ValidatorActions actions,
            Func <IRule, bool> ruleFilter
            )
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            var res = new BrokenRulesCollection();

            //获取指定实体的规则容器
            var rulesManager = ValidationHelper.GetTypeRules(target.FindRepository() as ITypeValidationsHost, target.GetType());

            if (rulesManager != null)
            {
                // get the rules list for this property
                RulesContainer rulesList = rulesManager.GetRulesForProperty(property, false);
                if (rulesList != null)
                {
                    // get the actual list of rules (sorted by priority)
                    CheckRules(target, rulesList, res, actions, ruleFilter);
                }
            }

            return(res);
        }
Esempio n. 2
0
        private static void CheckRules(
            Entity target,
            RulesContainer rules, BrokenRulesCollection brokenRulesList,
            ValidatorActions actions, Func <IRule, bool> ruleFilter
            )
        {
            var list = rules.GetList(true);

            var ignoreDataSourceValidations = HasAction(actions, ValidatorActions.IgnoreDataSourceValidations);
            var stopOnFirstBroken           = HasAction(actions, ValidatorActions.StopOnFirstBroken);

            for (int index = 0; index < list.Count; index++)
            {
                IRule rule = list[index];

                //连接数据源的验证规则可能需要被过滤掉。
                if (ignoreDataSourceValidations && rule.ValidationRule.ConnectToDataSource)
                {
                    continue;
                }

                //如果与指定的范围不符合,也需要过滤掉。
                if (ruleFilter != null && !ruleFilter(rule))
                {
                    continue;
                }

                //如果规则不适用于当前实体的状态,则也自动过滤掉。
                if (!IsMatchEntityStatus(target, rule.Meta))
                {
                    continue;
                }

                var args = new RuleArgs(rule);

                try
                {
                    args.BrokenDescription = null;
                    rule.ValidationRule.Validate(target, args);
                }
                catch (Exception ex)
                {
                    throw new ValidationException("Properties.Resources.ValidationRulesException" + args.Property.Name + rule.Key, ex);
                }

                if (args.IsBroken)
                {
                    brokenRulesList.Add(rule, args);

                    if (stopOnFirstBroken)
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 获取指定属性对应的属性规则容器。
        /// </summary>
        /// <param name="property">托管属性。</param>
        /// <param name="createList">如果还没有创建容器,是否需要同时创建该容器。</param>
        /// <returns></returns>
        internal RulesContainer GetRulesForProperty(IManagedProperty property, bool createList)
        {
            // get the list (if any) from the dictionary
            RulesContainer list = null;

            PropertyRules.TryGetValue(property, out list);

            if (createList && list == null)
            {
                // there is no list for this name - create one
                list = new RulesContainer();
                PropertyRules.Add(property, list);
            }

            return(list);
        }
Esempio n. 4
0
        private static void CheckRules(
            Entity target,
            RulesContainer rules, BrokenRulesCollection brokenRulesList,
            ValidatorActions actions, Func<IRule, bool> ruleFilter
            )
        {
            var list = rules.GetList(true);

            var ignoreDataSourceValidations = HasAction(actions, ValidatorActions.IgnoreDataSourceValidations);
            var stopOnFirstBroken = HasAction(actions, ValidatorActions.StopOnFirstBroken);

            for (int index = 0; index < list.Count; index++)
            {
                IRule rule = list[index];

                //连接数据源的验证规则可能需要被过滤掉。
                if (ignoreDataSourceValidations && rule.ValidationRule.ConnectToDataSource) { continue; }

                //如果与指定的范围不符合,也需要过滤掉。
                if (ruleFilter != null && !ruleFilter(rule)) { continue; }

                var args = new RuleArgs(rule);

                try
                {
                    args.BrokenDescription = null;
                    rule.ValidationRule.Validate(target, args);
                }
                catch (Exception ex)
                {
                    throw new ValidationException("Properties.Resources.ValidationRulesException" + args.Property.Name + rule.Key, ex);
                }

                if (args.IsBroken)
                {
                    brokenRulesList.Add(rule, args);

                    if (stopOnFirstBroken) { break; }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 获取指定属性对应的属性规则容器。
        /// </summary>
        /// <param name="property">托管属性。</param>
        /// <param name="createList">如果还没有创建容器,是否需要同时创建该容器。</param>
        /// <returns></returns>
        internal RulesContainer GetRulesForProperty(IManagedProperty property, bool createList)
        {
            // get the list (if any) from the dictionary
            RulesContainer list = null;
            PropertyRules.TryGetValue(property, out list);

            if (createList && list == null)
            {
                // there is no list for this name - create one
                list = new RulesContainer();
                PropertyRules.Add(property, list);
            }

            return list;
        }