You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Apewer;
  2. using Apewer.Internals;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. namespace Apewer.Source
  8. {
  9. /// <summary>数据库记录通用字段模型。</summary>
  10. /// <remarks>带有 Independent 特性的模型不包含此类型声明的属性。</remarks>
  11. [Serializable]
  12. public abstract class Record : IRecord
  13. {
  14. const int KeyLength = 32;
  15. private string _key = null;
  16. private long _flag = 0;
  17. /// <summary>记录主键,一般使用 GUID 的字符串形式。</summary>
  18. /// <remarks>
  19. /// <para>注:</para>
  20. /// <para>1. 默认长度为 32,需要修改长度时应该重写此属性;</para>
  21. /// <para>2. 带有 Independent 特性的模型不包含此属性。</para>
  22. /// </remarks>
  23. [Column("_key", ColumnType.NVarChar, KeyLength)]
  24. public virtual string Key { get { return _key; } set { _key = Compact(value, KeyLength); } }
  25. /// <summary>记录的标记,Int64 类型,区分记录的状态。</summary>
  26. /// <remarks>带有 Independent 特性的模型不包含此属性。</remarks>
  27. [Column("_flag", ColumnType.Integer)]
  28. public long Flag { get { return _flag; } set { _flag = value; } }
  29. /// <summary>重置 Key 属性的值。</summary>
  30. public virtual void ResetKey() => Key = TextUtility.Key();
  31. /// <summary></summary>
  32. public Record() => ResetKey();
  33. #region static
  34. /// <summary>枚举带有 Table 特性的 <typeparamref name="T"/> 派生类型。</summary>
  35. public static List<Type> EnumerateTableTypes<T>() where T : IRecord => EnumerateTableTypes(typeof(T));
  36. /// <summary>枚举带有 Table 特性的派生类型。</summary>
  37. public static List<Type> EnumerateTableTypes(Type baseType)
  38. {
  39. if (baseType == null) return new List<Type>();
  40. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  41. var list = new List<Type>();
  42. foreach (var a in assemblies)
  43. {
  44. var types = RuntimeUtility.GetTypes(a);
  45. foreach (var t in types)
  46. {
  47. if (!EnumerateTableTypes(t, baseType)) continue;
  48. if (list.Contains(t)) continue;
  49. list.Add(t);
  50. }
  51. }
  52. return list;
  53. }
  54. private static bool EnumerateTableTypes(Type type, Type @base)
  55. {
  56. if (type == null || @base == null) return false;
  57. if (!type.IsAbstract)
  58. {
  59. if (RuntimeUtility.Contains<TableAttribute>(type, false))
  60. {
  61. if (type.Equals(@base))
  62. {
  63. return true;
  64. }
  65. else
  66. {
  67. if (RuntimeUtility.IsInherits(type, @base)) return true; ;
  68. }
  69. }
  70. }
  71. return false;
  72. }
  73. // 限制文本长度,并去除两边的空白字符。
  74. static string Compact(string text, int length = -1)
  75. {
  76. if (string.IsNullOrEmpty(text)) return "";
  77. if (length > 0 && text.Length > length) text = text.Substring(0, length);
  78. return text.Trim();
  79. }
  80. #endregion
  81. #region 运算符。
  82. /// <summary>从 Record 到 Boolean 的隐式转换,判断 Record 对象不为 NULL。</summary>
  83. public static implicit operator bool(Record instance) => instance != null;
  84. #endregion
  85. }
  86. }