Пример #1
0
        internal static ValidationRulesManager GetTypeRules(ITypeValidationsHost host, Type entityType)
        {
            if (host == null)
            {
                //如果该类型没有 Repository,则使用默认的 TypeValidationsHost 来存储。
                host = TypeValidationsHost.FindOrCreate(entityType);
            }

            if (!host.TypeRulesAdded)
            {
                host.Rules          = new ValidationRulesManager();
                host.TypeRulesAdded = true;

                //在第一次创建时,添加类型的业务规则
                //注意,这个方法可能会调用到 Rules 属性获取刚才设置在 _typeRules 上的 ValidationRulesManager。
                InitializeValidations(entityType);

                //如果没有一个规则,则把这个属性删除。
                if (host.Rules.PropertyRules.Count == 0 && host.Rules.TypeRules.GetList(false).Count == 0)
                {
                    host.Rules = null;
                }
            }
            return(host.Rules);
        }