예제 #1
0
        public static void LoadValidateConfig()
        {
            var validateConfigFolder = ConfigurationManager.AppSettings["ValidateConfigFolder"] ?? "~/ValidateConfig";

            if (validateConfigFolder.IsNotNullOrEmpty())
            {
                validateConfigFolder = HttpContext.Current.Server.MapPath(validateConfigFolder);
                var directory = new DirectoryInfo(validateConfigFolder);
                if (directory.Exists)
                {
                    var files = directory.GetFiles("*.config");
                    foreach (var file in files)
                    {
                        try
                        {
                            var validateConfig = XMLUtility.CreateInstanceFromXml <ValidateConfig>(file.FullName);
                            if (validateConfigs.Any(c => c.EntityType == validateConfig.EntityType))
                            {
                                // 支持重复定义同类型的验证逻辑
                                validateConfigs.First(c => c.EntityType == validateConfig.EntityType)
                                .Fields.AddRange(validateConfig.Fields);
                            }
                            else
                            {
                                validateConfigs.Add(validateConfig);
                            }
                        }
                        catch (Exception)
                        {
                            // todo: log here
                        }
                    }
                }
            }

            if (validateConfigs.Count > 0)
            {
                GenerateEntityValidators();
            }

            isLoaded = true;
        }