//internal ValidationRulesManager GetTypeRules(ITypeValidationsHost host, Type entityType) //{ // //类型的规则,都放在 Repository 上。 // if (host == null) // { // //如果该类型没有 Repository,则使用默认的 TypeValidationsHost 来存储。 // host = TypeValidationsHost.FindOrCreate(entityType); // } // if (!host.TypeRulesAdded) // { // host.Rules = new ValidationRulesManager(); // host.TypeRulesAdded = true; // //在第一次创建时,添加类型的业务规则 // //注意,这个方法可能会调用到 Rules 属性获取刚才设置在 _typeRules 上的 ValidationRulesManager。 // AddValidations(); // //如果没有一个规则,则把这个属性删除。 // if (host.Rules.PropertyRules.Count == 0 && host.Rules.TypeRules.GetList(false).Count == 0) // { // host.Rules = null; // } // } // return host.Rules; //} internal static void InitializeValidations(Type entityType) { var declarer = new ValidationDeclarer(entityType); //为所有不可空的引用属性加上 Required 验证规则。 var container = ManagedPropertyRepository.Instance.GetTypePropertiesContainer(entityType); var properties = container.GetAvailableProperties(); foreach (var p in properties) { if (p is IRefIdProperty) { if (!(p as IRefIdProperty).Nullable) { declarer.AddRule(p, new RequiredRule()); } } } //同时,使用 EntityConfig 类来配置所有的验证规则。 var configurations = RafyEnvironment.FindConfigurations(entityType); foreach (var config in configurations) { //entityType 可能是子类型,所以需要传入对应的 declarer。 config.AddValidations(declarer); } }