예제 #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
파일: Validator.cs 프로젝트: yungtau/oea
        private static void ValidateEntity(Entity target, BrokenRulesCollection res, ValidatorActions actions, Func <IRule, bool> filter)
        {
            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, actions, filter);
                            if (stopOnFirst && res.Count > 0)
                            {
                                return(true);
                            }
                            return(false);
                        });
                        if (stopOnFirst && res.Count > 0)
                        {
                            return;
                        }
                    }
                    else
                    {
                        ValidateEntity(child.Value as Entity, res, actions, filter);
                        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
파일: Validator.cs 프로젝트: yungtau/oea
        /// <summary>
        /// 检查整个实体对象是否满足规则
        /// </summary>
        /// <param name="target">要验证的实体。</param>
        /// <param name="actions">验证时的行为。</param>
        /// <param name="ruleFilter">要验证的规则的过滤器。</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">target</exception>
        public static BrokenRulesCollection Validate(
            this Entity target,
            ValidatorActions actions,
            Func <IRule, bool> ruleFilter
            )
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            var res = new BrokenRulesCollection();

            ValidateEntity(target, res, actions, ruleFilter);

            return(res);
        }
예제 #5
0
파일: Validator.cs 프로젝트: 569550384/Rafy
 /// <summary>
 /// 检查某个属性是否满足规则
 /// </summary>
 /// <param name="target">要验证的实体。</param>
 /// <param name="property">托管属性</param>
 /// <param name="actions">验证时的行为。</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">target
 /// or
 /// property</exception>
 public static BrokenRulesCollection Validate(this Entity target, IManagedProperty property, ValidatorActions actions)
 {
     return Validate(target, property, actions, null);
 }
예제 #6
0
파일: Validator.cs 프로젝트: 569550384/Rafy
        private static void ValidateEntity(Entity target, BrokenRulesCollection res, ValidatorActions actions, Func<IRule, bool> filter)
        {
            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, actions, filter);
                            if (stopOnFirst && res.Count > 0) return true;
                            return false;
                        });
                        if (stopOnFirst && res.Count > 0) return;
                    }
                    else
                    {
                        ValidateEntity(child.Value as Entity, res, actions, filter);
                        if (stopOnFirst && res.Count > 0) return;
                    }
                }
            }
        }
예제 #7
0
파일: Validator.cs 프로젝트: 569550384/Rafy
 private static bool HasAction(ValidatorActions actions, ValidatorActions toCheck)
 {
     return (actions & toCheck) == toCheck;
 }
예제 #8
0
파일: Validator.cs 프로젝트: 569550384/Rafy
        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; }
                }
            }
        }
예제 #9
0
파일: Validator.cs 프로젝트: 569550384/Rafy
        /// <summary>
        /// 检查整个实体对象是否满足规则
        /// </summary>
        /// <param name="target">要验证的实体。</param>
        /// <param name="actions">验证时的行为。</param>
        /// <param name="ruleFilter">要验证的规则的过滤器。</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">target</exception>
        public static BrokenRulesCollection Validate(
            this Entity target,
            ValidatorActions actions,
            Func<IRule, bool> ruleFilter
            )
        {
            if (target == null) throw new ArgumentNullException("target");

            var res = new BrokenRulesCollection();

            ValidateEntity(target, res, actions, ruleFilter);

            return res;
        }
예제 #10
0
파일: Validator.cs 프로젝트: 569550384/Rafy
 /// <summary>
 /// 检查整个实体对象是否满足规则
 /// </summary>
 /// <param name="target">要验证的实体。</param>
 /// <param name="actions">验证时的行为。</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">target</exception>
 public static BrokenRulesCollection Validate(this Entity target, ValidatorActions actions)
 {
     return Validate(target, actions, null);
 }
예제 #11
0
파일: Validator.cs 프로젝트: 569550384/Rafy
        /// <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;
        }
예제 #12
0
파일: Validator.cs 프로젝트: hardCTE/Rafy
 /// <summary>
 /// 检查整个实体对象是否满足规则
 /// </summary>
 /// <param name="target">要验证的实体。</param>
 /// <param name="actions">验证时的行为。</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">target</exception>
 public static BrokenRulesCollection Validate(this Entity target, ValidatorActions actions)
 {
     return Validate(target, null as Func<IRule, bool>, actions);
 }
예제 #13
0
 /// <summary>
 /// 检查某个属性是否满足规则
 /// </summary>
 /// <param name="target">要验证的实体。</param>
 /// <param name="property">托管属性</param>
 /// <param name="actions">验证时的行为。</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">target
 /// or
 /// property</exception>
 public static BrokenRulesCollection Validate(this Entity target, IManagedProperty property, ValidatorActions actions)
 {
     return(Validate(target, property, actions, null));
 }
예제 #14
0
 private static bool HasAction(ValidatorActions actions, ValidatorActions toCheck)
 {
     return((actions & toCheck) == toCheck);
 }
예제 #15
0
 /// <summary>
 /// 检查整个实体对象是否满足规则
 /// </summary>
 /// <param name="target">要验证的实体。</param>
 /// <param name="actions">验证时的行为。</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">target</exception>
 public static BrokenRulesCollection Validate(this Entity target, ValidatorActions actions)
 {
     return(Validate(target, null as Func <IRule, bool>, actions));
 }
예제 #16
0
파일: Validator.cs 프로젝트: yungtau/oea
 /// <summary>
 /// 检查整个实体对象是否满足规则
 /// </summary>
 /// <param name="target">要验证的实体。</param>
 /// <param name="actions">验证时的行为。</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException">target</exception>
 public static BrokenRulesCollection Validate(this Entity target, ValidatorActions actions)
 {
     return(Validate(target, actions, null));
 }