/// <summary> /// 数据验证 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dr">DataRow</param> internal static void Validate <T>(this DataRow dr) where T : BaseEntity, new() { if (dr.RowState == DataRowState.Deleted) { return; } string ClassName = typeof(T).Name; if (!DataCache.AttributeCache.TryGetValue(ClassName, out var PropAttrs)) { return; } List <PropertyInfo> props = MemberInfoEx.GetPropertyCache <T>(); List <ErrorInfo> Errs = new List <ErrorInfo>(); foreach (PropertyInfo prop in props) { if (!PropAttrs.TryGetValue(prop.Name, out var attributes)) { continue; } foreach (CustomBaseAttribute attr in attributes) { if (attr is FieldAttribute FieldAttr) { string FieldName = string.IsNullOrEmpty(DataCache.CustomNameCache[ClassName + "|" + prop.Name]) ? prop.Name : DataCache.CustomNameCache[ClassName + "|" + prop.Name]; string Value = dr[prop.GetCustomName()] + ""; if (string.IsNullOrEmpty(Value) && !FieldAttr.Empty) { Errs.Add(new ErrorInfo(FieldName, "不能为空!")); } if (FieldAttr.Length > 0) { if (Value.Length > FieldAttr.Length) { Errs.Add(new ErrorInfo(FieldName, string.IsNullOrEmpty(FieldAttr.ErrorMessage) ? "长度超出限定范围,范围:" + FieldAttr.Length : FieldAttr.ErrorMessage)); } } } } } if (Errs.Count > 0) { throw new SqlFieldsException(Errs); } }
/// <summary> /// Получить словарь атрибутов свойств /// </summary> private static Dictionary <string, PropAttrs> GetPropAttrsDict(Localization.Dict dict) { Dictionary <string, PropAttrs> propAttrsDict = new Dictionary <string, PropAttrs>(); foreach (string phraseKey in dict.Phrases.Keys) { string phraseVal = dict.Phrases[phraseKey]; int dotPos = phraseKey.IndexOf('.'); if (0 < dotPos && dotPos < phraseKey.Length - 1) { // если точка в середине ключа фразы, то слева от точки - имя свойства, справа - имя атрибута string propName = phraseKey.Substring(0, dotPos); string attrName = phraseKey.Substring(dotPos + 1); bool attrAssigned = true; PropAttrs propAttrs; if (!propAttrsDict.TryGetValue(propName, out propAttrs)) { propAttrs = new PropAttrs(); } if (attrName == "DisplayName") { propAttrs.DisplayName = phraseVal; } else if (attrName == "Category") { propAttrs.Category = phraseVal; } else if (attrName == "Description") { propAttrs.Description = phraseVal; } else { attrAssigned = false; } if (attrAssigned) { propAttrsDict[propName] = propAttrs; } } } return(propAttrsDict); }
/// <summary> /// Builds a dictionary that contains attribute values accessed by property name. /// </summary> private static Dictionary <string, PropAttrs> BuildPropAttrsDict(LocaleDict dict) { Dictionary <string, PropAttrs> propAttrsDict = new Dictionary <string, PropAttrs>(); foreach (string phraseKey in dict.Phrases.Keys) { string phraseVal = dict.Phrases[phraseKey]; int dotIdx = phraseKey.IndexOf('.'); if (0 < dotIdx && dotIdx < phraseKey.Length - 1) { // PropertyName.AttributeName string propName = phraseKey.Substring(0, dotIdx); string attrName = phraseKey.Substring(dotIdx + 1); bool itemFound = propAttrsDict.TryGetValue(propName, out PropAttrs propAttrs); if (!itemFound) { propAttrs = new PropAttrs(); propAttrsDict.Add(propName, propAttrs); } if (attrName == "DisplayName") { propAttrs.DisplayName = phraseVal; } else if (attrName == "Category") { propAttrs.Category = phraseVal; } else if (attrName == "Description") { propAttrs.Description = phraseVal; } } } return(propAttrsDict); }