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.

88 lines
2.9 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
  1. using Apewer.Internals;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Apewer.Source
  6. {
  7. /// <summary>数据库中的表。</summary>
  8. /// <remarks>
  9. /// <para>Name: 数据库的表名。</para>
  10. /// <para>Store: 数据存储区名称。</para>
  11. /// </remarks>
  12. [Serializable]
  13. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = true)]
  14. public sealed class TableAttribute : Attribute
  15. {
  16. private string _name = null;
  17. private string _store = null;
  18. /// <summary>标记表属性。</summary>
  19. public TableAttribute(string name = null, string store = null)
  20. {
  21. _name = name;
  22. _store = store;
  23. }
  24. /// <summary>表名。</summary>
  25. public string Name { get => _name; }
  26. /// <summary>存储名。</summary>
  27. public string Store { get => _store; }
  28. /// <summary>表的说明信息。(需要数据库客户端支持)</summary>
  29. public string Description { get; set; }
  30. /// <summary>独立结构,不依赖 Record 公共属性。</summary>
  31. internal bool Independent { get; set; }
  32. /// <summary>使用模型的所有属性,对缺少 Column 特性的属性使用默认参数的 Column 特性。</summary>
  33. public bool AllProperties { get; set; }
  34. private static Dictionary<string, TableAttribute> _tac = new Dictionary<string, TableAttribute>();
  35. /// <summary>解析表特性,默认使用缓存以提升性能。</summary>
  36. public static TableAttribute Parse<T>(bool useCache = true) where T: class, new() => Parse(typeof(T), useCache);
  37. /// <summary>解析表特性,默认使用缓存以提升性能。</summary>
  38. public static TableAttribute Parse(Type type, bool useCache = true)
  39. {
  40. var cacheKey = type.FullName;
  41. if (useCache)
  42. {
  43. var hint = null as TableAttribute;
  44. lock (_tac)
  45. {
  46. if (_tac.ContainsKey(cacheKey))
  47. {
  48. hint = _tac[cacheKey];
  49. }
  50. }
  51. if (hint != null) return hint;
  52. }
  53. // throw new Exception($"类型 {type.FullName} 不包含 {typeof(TableAttribute).FullName}。");
  54. var tas = type.GetCustomAttributes(typeof(TableAttribute), false);
  55. if (tas.LongLength < 1L) return null;
  56. var ta = (TableAttribute)tas[0];
  57. if (string.IsNullOrEmpty(ta.Name)) ta._name = type.Name;
  58. ta.Independent = RuntimeUtility.Contains<IndependentAttribute>(type, true);
  59. if (useCache)
  60. {
  61. lock (_tac)
  62. {
  63. if (!_tac.ContainsKey(cacheKey)) _tac.Add(cacheKey, ta);
  64. }
  65. }
  66. return ta;
  67. }
  68. }
  69. }