using System; namespace Phenix.Core.Mapping { /// <summary> /// "数据映射字段业务规则"标签 /// </summary> [Serializable()] [AttributeUsage(AttributeTargets.Field)] public sealed class FieldRuleAttribute : Attribute { #region 属性 private static string _defaultUpperCaseFriendlyName = "代码"; /// <summary> /// 缺省可以强制转换成大写的 FriendlyName 的匹配子字符串, 可以';'分隔 /// </summary> public static string DefaultUpperCaseFriendlyName { get { return _defaultUpperCaseFriendlyName; } set { _defaultUpperCaseFriendlyName = value; } } private static string _defaultOnImeModeFriendlyName = "中文名"; /// <summary> /// 缺省可以输入中文的 FriendlyName 的匹配子字符串, 可以';'分隔 /// </summary> public static string DefaultOnImeModeFriendlyName { get { return _defaultOnImeModeFriendlyName; } set { _defaultOnImeModeFriendlyName = value; } } #region 字符串 private bool? _stringUpperCase; /// <summary> /// 指示该字段内容是否强制转换成大写(true 为 CharacterCasing.Upper, false 为 CharacterCasing.Lower, 不赋值的情况下为 CharacterCasing.Normal) /// 当 FriendlyName 包含 DefaultUpperCaseFriendlyName 时不必在此申明 /// 为 Phenix.Services.Client.Validation.ResetEditCharacterCasing() 实现其功能 /// </summary> public bool StringUpperCase { get { return _stringUpperCase.Value; } set { _stringUpperCase = value; } } public bool StringUpperCaseHasValue { get { return _stringUpperCase.HasValue; } } private bool? _stringOnImeMode; /// <summary> /// 指示该字段内容是否可以输入中文(true 为 ImeMode.On, false 为 ImeMode.Off, 不赋值的情况下为 ImeMode.Inherit) /// 当 FriendlyName 包含 DefaultOnImeModeFriendlyName 时不必在此申明 /// 为 Phenix.Services.Client.Validation.ResetEditImeMode() 实现其功能 /// </summary> public bool StringOnImeMode { get { return _stringOnImeMode.Value; } set { _stringOnImeMode = value; } } public bool StringOnImeModeHasValue { get { return _stringOnImeMode.HasValue; } } private int? _stringMaxLength; /// <summary> /// 允许字符串最大长度 /// </summary> public int StringMaxLength { get { return _stringMaxLength.Value; } set { _stringMaxLength = value; } } public bool MaxLengthHasValue { get { return _stringMaxLength.HasValue; } } private int? _stringMinLength; /// <summary> /// 允许字符串最小长度 /// </summary> public int StringMinLength { get { return _stringMinLength.Value; } set { _stringMinLength = value; } } public bool StringMinLengthHasValue { get { return _stringMinLength.HasValue; } } #endregion #region 数字 private decimal? _numberMaxValue; /// <summary> /// 允许数字最大值 /// </summary> public decimal NumberMaxValue { get { return _numberMaxValue.Value; } set { _numberMaxValue = value; } } public bool NumberMaxValueHasValue { get { return _numberMaxValue.HasValue; } } private decimal? _numberMinValue; /// <summary> /// 允许数字最小值 /// </summary> public decimal NumberMinValue { get { return _numberMinValue.Value; } set { _numberMinValue = value; } } public bool NumberMinValueHasValue { get { return _numberMinValue.HasValue; } } #endregion #region 时间 private TimeSpan? _dateTimeStartTimeSpan; /// <summary> /// 允许时间范围起始值(相对当前时间) /// </summary> public TimeSpan DateTimeStartTimeSpan { get { return _dateTimeStartTimeSpan.Value; } } public bool DateTimeStartTimeSpanHasValue { get { return _dateTimeStartTimeSpan.HasValue; } } /// <summary> /// 允许时间范围起始值(相对当前时间), 指定天数 /// </summary> public double DateTimeStartTimeSpanFromDays { get { return _dateTimeStartTimeSpan.Value.TotalDays; } set { _dateTimeStartTimeSpan = TimeSpan.FromDays(value); } } /// <summary> /// 允许时间范围起始值(相对当前时间), 指定小时数 /// </summary> public double DateTimeStartTimeSpanFromHours { get { return _dateTimeStartTimeSpan.Value.TotalHours; } set { _dateTimeStartTimeSpan = TimeSpan.FromHours(value); } } /// <summary> /// 允许时间范围起始值(相对当前时间), 指定分钟数 /// </summary> public double DateTimeStartTimeSpanFromMinutes { get { return _dateTimeStartTimeSpan.Value.TotalMinutes; } set { _dateTimeStartTimeSpan = TimeSpan.FromMinutes(value); } } private TimeSpan? _dateTimeEndTimeSpan; /// <summary> /// 允许时间范围结束值(相对当前时间) /// </summary> public TimeSpan DateTimeEndTimeSpan { get { return _dateTimeEndTimeSpan.Value; } } public bool DateTimeEndTimeSpanHasValue { get { return _dateTimeEndTimeSpan.HasValue; } } /// <summary> /// 允许时间范围结束值(相对当前时间), 指定天数 /// </summary> public double DateTimeEndTimeSpanFromDays { get { return _dateTimeEndTimeSpan.Value.TotalDays; } set { _dateTimeEndTimeSpan = TimeSpan.FromDays(value); } } /// <summary> /// 允许时间范围结束值(相对当前时间), 指定小时数 /// </summary> public double DateTimeEndTimeSpanFromHours { get { return _dateTimeEndTimeSpan.Value.TotalHours; } set { _dateTimeEndTimeSpan = TimeSpan.FromHours(value); } } /// <summary> /// 允许时间范围结束值(相对当前时间), 指定分钟数 /// </summary> public double DateTimeEndTimeSpanFromMinutes { get { return _dateTimeEndTimeSpan.Value.TotalMinutes; } set { _dateTimeEndTimeSpan = TimeSpan.FromMinutes(value); } } #endregion #endregion } } 在类字段上定义上面的校验规则后,框架自动将这些规则注入到业务类的规则库里。 这种方式等效于自己写代码添加校验规则: /// <summary> /// 程序集类 /// </summary> [Serializable()] [Phenix.Core.Mapping.Class("PH_AssemblyClass")] public class AssemblyClass : Phenix.Business.BusinessBase<AssemblyClass> { .... private static Csla.PropertyInfo<string> _AC_NameProperty = RegisterProperty<string>(typeof(AssemblyClass), new Csla.PropertyInfo<string>("AC_Name", "名称")); [Phenix.Core.Mapping.Field(NeedUpdate = true, FriendlyName = "名称")] //[Phenix.Core.Mapping.FieldRule(StringMaxLength = 30, StringMinLength = 10)] private string _AC_Name; /// <summary> /// 名称 /// </summary> public string AC_Name { get { return GetProperty<string>(_AC_NameProperty, _AC_Name); } set { SetProperty<string>(_AC_NameProperty, ref _AC_Name, value); } } ... /// <summary> /// 添加校验规则 /// </summary> protected override void AddBusinessRules() { base.AddBusinessRules(); //与 [Phenix.Core.Mapping.FieldRule(StringMaxLength = 30, StringMinLength = 10)] 作用等效 ValidationRules.AddRule(Phenix.Business.Validation.CommonRules.StringMaxLength, new Phenix.Business.Validation.CommonRules.StringMaxLengthRuleArgs(_AC_NameProperty.PropertyName, _AC_NameProperty.FriendlyName, 30)); ValidationRules.AddRule(Phenix.Business.Validation.CommonRules.StringMinLength, new Phenix.Business.Validation.CommonRules.StringMinLengthRuleArgs(_AC_NameProperty.PropertyName, _AC_NameProperty.FriendlyName, 10)); } } } 当然这么做毕竟没有用FieldRuleAttribute来的更加简洁明了。