此示例所示的 RuleRequiredFieldPersistent 類是一個普通的業務類。類實現 IRuleSource 接口,並用於創建和存儲在數據庫中的 RuleRequiredField 驗證規則。在 CreateRules 方法中實例化一個 RuleRequiredField 驗證規則,基於 RuleRequiredFieldPersistent 類的公共屬性的值。RuleRequiredFieldPersistent 類標記 DefaultClassOptionsAttribute,以便最終用戶可以手動創建驗證規則通過相應的列表視圖。
[DefaultClassOptions] public class RuleRequiredFieldPersistent : BaseObject, DevExpress.Persistent.Validation.IRuleSource { public RuleRequiredFieldPersistent(Session session) : base(session) { } public string RuleName { get { return GetPropertyValue<string>("RuleName"); } set { SetPropertyValue("RuleName", value); } } public string CustomMessageTemplate { get { return GetPropertyValue<string>("CustomMessageTemplate"); } set { SetPropertyValue("CustomMessageTemplate", value); } } public bool SkipNullOrEmptyValues { get { return GetPropertyValue<bool>("SkipNullOrEmptyValues"); } set { SetPropertyValue("SkipNullOrEmptyValues", value); } } public string Id { get { return GetPropertyValue<string>("Id"); } set { SetPropertyValue("Id", value); } } public bool InvertResult { get { return GetPropertyValue<bool>("InvertResult"); } set { SetPropertyValue("InvertResult", value); } } public string ContextIDs { get { return GetPropertyValue<string>("ContextIDs"); } set { SetPropertyValue("ContextIDs", value); } } public string Property { get { return GetPropertyValue<string>("Property"); } set { SetPropertyValue("Property", value); } } [Persistent("ObjectType")] protected string ObjectType { get { if(ObjectTypeCore != null) { return ObjectTypeCore.FullName; } return ""; } set { ObjectTypeCore = ReflectionHelper.FindType(value); } } [NonPersistent] [TypeConverter(typeof(DevExpress.Persistent.Base.LocalizedClassInfoTypeConverter))] public Type ObjectTypeCore { get { return GetPropertyValue<Type>("ObjectTypeCore"); } set { SetPropertyValue("ObjectTypeCore", value); } } #region IRuleSource Members public System.Collections.Generic.ICollection<IRule> CreateRules() { System.Collections.Generic.List<IRule> list = new System.Collections.Generic.List<IRule>(); RuleRequiredField rule = new RuleRequiredField(); rule.Properties.SkipNullOrEmptyValues = this.SkipNullOrEmptyValues; rule.Properties.Id = this.Id; rule.Properties.InvertResult = this.InvertResult; rule.Properties.CustomMessageTemplate = this.CustomMessageTemplate; rule.Properties.TargetContextIDs = new ContextIdentifiers(this.ContextIDs); rule.Properties.TargetType = this.ObjectTypeCore; if(rule.Properties.TargetType != null) { foreach(PropertyInfo pi in rule.Properties.TargetType.GetProperties()) { if(pi.Name == this.Property) { rule.Properties.TargetPropertyName = pi.Name; } } } for(int i = Validator.RuleSet.RegisteredRules.Count - 1; i >= 0; i--) { if(Validator.RuleSet.RegisteredRules[i].Id == this.Id) { Validator.RuleSet.RegisteredRules.RemoveAt(i); } } list.Add(rule); return list; } [Browsable(false)] public string Name { get { return this.RuleName; } } #endregion }
可以看到,這個示例中,只返回了一個規則,而在實際項目中,可以使用BO定義一個子集合,集合中定義N種規則。
不要定義N個BO並都實現 IRuleSource 那樣有點浪費。