CheckData() public method

数据校验方法
public CheckData ( ) : string
return string
Esempio n. 1
0
        void CheckData(IModel obj)
        {
            var    types = CRL.TypeCache.GetProperties(obj.GetType(), true).Values;
            string msg;

            //检测数据约束
            foreach (Attribute.FieldAttribute p in types)
            {
                if (p.PropertyType == typeof(System.String))
                {
                    string value = p.GetValue(obj) + "";
                    if (p.NotNull && string.IsNullOrEmpty(value))
                    {
                        msg = string.Format("对象{0}属性{1}值不能为空", obj.GetType(), p.Name);
                        throw new Exception(msg);
                    }
                    if (value.Length > p.Length && p.Length < 3000)
                    {
                        msg = string.Format("对象{0}属性{1}长度超过了设定值{2}", obj.GetType(), p.Name, p.Length);
                        throw new Exception(msg);
                    }
                }
            }
            //校验数据
            msg = obj.CheckData();
            if (!string.IsNullOrEmpty(msg))
            {
                msg = string.Format("数据校验证失败,在类型{0} {1} 请核对校验规则", obj.GetType(), msg);
                throw new Exception(msg);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 检测数据
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="checkRepeated"></param>
        internal void CheckData(IModel obj, bool checkRepeated)
        {
            //var types = CRL.TypeCache.GetProperties(obj.GetType(), true).Values;
            var    types = TypeCache.GetTable(obj.GetType()).Fields;
            string msg;
            var    sb = new StringBuilder();

            //检测数据约束
            foreach (Attribute.FieldInnerAttribute p in types)
            {
                string value = p.GetValue(obj) + "";
                if (!string.IsNullOrEmpty(value) && p.MemberName != "AddTime" && checkRepeated)
                {
                    sb.Append(value.GetHashCode().ToString());
                }
                if (p.PropertyType == typeof(System.String))
                {
                    if (p.NotNull && string.IsNullOrEmpty(value))
                    {
                        msg = string.Format("对象{0}属性{1}值不能为空", obj.GetType(), p.MemberName);
                        throw new CRLException(msg);
                    }
                    if (value.Length > p.Length && p.Length < 3000)
                    {
                        msg = string.Format("对象{0}属性{1}长度超过了设定值{2}[{3}]", obj.GetType(), p.MemberName, p.Length, value);
                        throw new CRLException(msg);
                    }
                }
            }
            if (checkRepeated)
            {
                //string concurrentKey = "insertRepeatedCheck_" + StringHelper.EncryptMD5(sb.ToString());
                string concurrentKey = "insertRepeatedCheck_" + sb.ToString().GetHashCode();
                if (!ConcurrentControl.Check(concurrentKey, 1))
                {
                    throw new CRLException("检测到有重复提交的数据,在" + obj.GetType());
                }
            }
            //校验数据
            msg = obj.CheckData();
            if (!string.IsNullOrEmpty(msg))
            {
                msg = string.Format("数据校验证失败,在类型{0} {1} 请核对校验规则", obj.GetType(), msg);
                throw new CRLException(msg);
            }
        }