using Apewer; using Apewer.Internals; using System; using System.Collections; using System.Collections.Generic; using System.Reflection; namespace Apewer.Source { /// 数据库记录通用字段模型。 /// 带有 Independent 特性的模型不包含此类型声明的属性。 [Serializable] public abstract class Record : IRecord { const int KeyLength = 191; private string _key = null; private long _flag = 0; /// 记录主键,一般使用 GUID 的字符串形式。 /// 带有 Independent 特性的模型不包含此属性。 [Column("_key", ColumnType.NVarChar, KeyLength)] public string Key { get { return _key; } set { _key = Compact(value, KeyLength); } } /// 记录的标记,Int64 类型,区分记录的状态。 /// 带有 Independent 特性的模型不包含此属性。 [Column("_flag", ColumnType.Integer)] public long Flag { get { return _flag; } set { _flag = value; } } /// 重置 Key 属性的值。 public virtual void ResetKey() => _key = TextUtility.Key(); /// public Record() => ResetKey(); #region static /// 枚举带有 Table 特性的 派生类型。 public static List EnumerateTableTypes() where T : IRecord => EnumerateTableTypes(typeof(T)); /// 枚举带有 Table 特性的派生类型。 public static List EnumerateTableTypes(Type baseType) { if (baseType == null) return new List(); var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var list = new List(); foreach (var a in assemblies) { var types = RuntimeUtility.GetTypes(a); foreach (var t in types) { if (!EnumerateTableTypes(t, baseType)) continue; if (list.Contains(t)) continue; list.Add(t); } } return list; } private static bool EnumerateTableTypes(Type type, Type @base) { if (type == null || @base == null) return false; if (!type.IsAbstract) { if (RuntimeUtility.Contains(type, false)) { if (type.Equals(@base)) { return true; } else { if (RuntimeUtility.IsInherits(type, @base)) return true; ; } } } return false; } // 限制文本长度,并去除两边的空白字符。 static string Compact(string text, int length = -1) { if (string.IsNullOrEmpty(text)) return ""; if (length > 0 && text.Length > length) text = text.Substring(0, length); return text.Trim(); } #endregion #region 运算符。 /// 从 Record 到 Boolean 的隐式转换,判断 Record 对象不为 NULL。 public static implicit operator bool(Record instance) => instance != null; #endregion } }