示例#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);
        }
示例#2
0
        private static void ValidateEntity(Entity target, BrokenRulesCollection res, Func <IRule, bool> filter, ValidatorActions actions)
        {
            var stopOnFirst = HasAction(actions, ValidatorActions.StopOnFirstBroken);

            var rules = ValidationHelper.GetTypeRules(target.FindRepository() as ITypeValidationsHost, target.GetType());

            if (rules != null)
            {
                //先验证所有属性。
                foreach (var de in rules.PropertyRules)
                {
                    CheckRules(target, de.Value, res, actions, filter);
                    if (stopOnFirst && res.Count > 0)
                    {
                        return;
                    }
                }

                //再验证整个实体。
                CheckRules(target, rules.TypeRules, res, actions, filter);
                if (stopOnFirst && res.Count > 0)
                {
                    return;
                }
            }

            if (HasAction(actions, ValidatorActions.ValidateChildren))
            {
                foreach (var child in target.GetLoadedChildren())
                {
                    var list = child.Value as EntityList;
                    if (list != null)
                    {
                        list.EachNode(childEntity =>
                        {
                            ValidateEntity(childEntity, res, filter, actions);
                            if (stopOnFirst && res.Count > 0)
                            {
                                return(true);
                            }
                            return(false);
                        });
                        if (stopOnFirst && res.Count > 0)
                        {
                            return;
                        }
                    }
                    else
                    {
                        ValidateEntity(child.Value as Entity, res, filter, actions);
                        if (stopOnFirst && res.Count > 0)
                        {
                            return;
                        }
                    }
                }
            }
        }
示例#3
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;
                    }
                }
            }
        }
示例#4
0
        ///// <summary>
        ///// 检查整个实体对象是否满足规则
        ///// </summary>
        ///// <param name="target">要验证的实体。</param>
        ///// <param name="scope">要验证的实体的状态范围。</param>
        ///// <returns></returns>
        ///// <exception cref="System.ArgumentNullException">target</exception>
        //public static BrokenRulesCollection Validate(this Entity target, EntityStatusScopes scope)
        //{
        //    return Validate(target, r => r.Meta.HasScope(scope), DefaultActions);
        //}

        ///// <summary>
        ///// 检查整个实体对象是否满足规则
        ///// </summary>
        ///// <param name="target">要验证的实体。</param>
        ///// <param name="scope">要验证的实体的状态范围。</param>
        ///// <param name="actions">要验证的行为.</param>
        ///// <returns></returns>
        ///// <exception cref="System.ArgumentNullException">target</exception>
        //public static BrokenRulesCollection Validate(this Entity target, EntityStatusScopes scope, ValidatorActions actions)
        //{
        //    return Validate(target, r => r.Meta.HasScope(scope), actions);
        //}

        /// <summary>
        /// 检查整个实体对象是否满足规则
        /// </summary>
        /// <param name="target">要验证的实体。</param>
        /// <param name="ruleFilter">要验证的规则的过滤器。</param>
        /// <param name="actions">验证时的行为。</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">target</exception>
        public static BrokenRulesCollection Validate(this Entity target, Func <IRule, bool> ruleFilter, ValidatorActions actions)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            var res = new BrokenRulesCollection();

            ValidateEntity(target, res, ruleFilter, actions);

            return(res);
        }