Ejemplo n.º 1
0
        public void Bind(Type modelType, string propertyName, ISettingsBindable settings)
        {
            //模型类型必须实现自 IEntity
            if (!typeof(IEntity).IsAssignableFrom(modelType))
            {
                return;
            }

            //获取对应的依赖属性
            var property = PropertyUnity.GetProperty(modelType, propertyName);

            if (property == null)
            {
                return;
            }

            var vsettings = settings as ValidateBoxSettings;
            //获取依赖属性所指定的验证特性
            var validTypes = new List <string>();

            foreach (var validation in ValidationUnity.GetValidations(property))
            {
                ParseValidation(vsettings, validation, validTypes);
            }

            vsettings.ValidType = validTypes.ToArray();
        }
Ejemplo n.º 2
0
        public void GetValidationsTest()
        {
            ValidationUnity.RegisterValidation(typeof(Products), new ProductQuantityValidateAttribute());
            var validations = ValidationUnity.GetValidations(typeof(Products));

            Console.WriteLine(validations.Count());
        }
Ejemplo n.º 3
0
        public void Bind(Type modelType, string propertyName, ISettingsBindable settings)
        {
            var nsettings = settings as NumberBoxSettings;

            if (!typeof(IEntity).IsAssignableFrom(modelType))
            {
                return;
            }

            //获取对应的依赖属性
            var property = PropertyUnity.GetProperty(modelType, propertyName);

            if (property == null)
            {
                return;
            }

            nsettings.Precision = property.Info.Scale;

            //找 RangeAttribute 特性
            var range = ValidationUnity.GetValidations(property).Where(s => s is RangeAttribute).Cast <RangeAttribute>().FirstOrDefault();

            if (range != null)
            {
                nsettings.Min = range.Minimum.To <decimal?>();
                nsettings.Max = range.Maximum.To <decimal?>();
            }
        }
Ejemplo n.º 4
0
        public void GetValidationsForPropertyTest()
        {
            var p           = PropertyUnity.GetProperty(typeof(Products), "ProductName");
            var validations = ValidationUnity.GetValidations(p);

            Console.WriteLine(validations.Count());
        }
Ejemplo n.º 5
0
        public void GetPropertyValidationsTest()
        {
            var p = PropertyUnity.GetProperty(typeof(Orders), "CustomerID");

            ValidationUnity.RegisterValidation(p, new ProductQuantityValidateAttribute());
            var validations = ValidationUnity.GetValidations(p);

            Console.WriteLine(validations.Count());
        }
Ejemplo n.º 6
0
 public void GetValidationsForPropertyParallelTest()
 {
     Parallel.For(0, 100, (i) =>
     {
         var p           = PropertyUnity.GetProperty(typeof(Products), "ProductName");
         var validations = ValidationUnity.GetValidations(p);
         Console.WriteLine(validations.Count());
     });
 }
Ejemplo n.º 7
0
 public void GetValidationsParallelTest()
 {
     ValidationUnity.RegisterValidation(typeof(Products), new ProductQuantityValidateAttribute());
     Parallel.For(0, 100, (i) =>
     {
         var validations = ValidationUnity.GetValidations(typeof(Products));
         Console.WriteLine(validations.Count());
     });
 }
Ejemplo n.º 8
0
        /// <summary>
        /// 输出实体验证特性。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="html"></param>
        /// <returns></returns>
        public static IHtmlString AddValidations <T>(this HtmlHelper html)
        {
            var dict = new Dictionary <string, IEnumerable <string> >();

            foreach (var property in PropertyUnity.GetPersistentProperties(typeof(T)))
            {
                var validations = ValidationUnity.GetValidations(property).Select(s => GetValidation(s)).Where(s => !string.IsNullOrEmpty(s)).ToList();
                if (validations.Count > 0)
                {
                    dict.Add(property.Name, validations);
                }
            }

            return(MvcHtmlString.Create(new JsonSerializer().Serialize(dict)));
        }