TableRepository.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using XGame.Framework.Asyncs;
  4. using XGame.Framework.Serialization;
  5. namespace XGame.Framework.Database
  6. {
  7. public abstract class TableRepository<TTable, TRepository> : ISerializable, IDisposable where TTable : class, ITable where TRepository : TableRepository<TTable, TRepository>
  8. {
  9. protected TTable[] _tables;
  10. protected Dictionary<long, TTable> _tableMap;
  11. void ISerializable.Deserialize(IReader reader)
  12. {
  13. _tables = reader.ReadEnumerable<TTable[]>();
  14. _tableMap = new Dictionary<long, TTable>();
  15. foreach(var table in _tables)
  16. {
  17. #if UNITY_EDITOR
  18. if (_tableMap.ContainsKey(table.Key))
  19. {
  20. Log.Error($"TableRepository 重复数据. Name:{typeof(TTable)} Id:{table.Key}");
  21. continue;
  22. }
  23. #endif
  24. _tableMap.Add(table.Key, table);
  25. }
  26. }
  27. void ISerializable.Serialize(IWriter writer)
  28. {
  29. writer.Write(_tables);
  30. }
  31. void IDisposable.Dispose()
  32. {
  33. _tables = null;
  34. _tableMap?.Clear();
  35. _tableMap = null;
  36. }
  37. public TTable GetByKey(long key)
  38. {
  39. if (_tableMap == null)
  40. {
  41. return default;
  42. }
  43. _tableMap.TryGetValue(key, out var table);
  44. return table;
  45. }
  46. /// <summary>
  47. /// 根据索引取值
  48. /// index == -1 表示取最后一个
  49. /// </summary>
  50. /// <param name="index"></param>
  51. /// <returns></returns>
  52. public TTable GetByIndex(int index)
  53. {
  54. var count = _tables?.Length ?? 0;
  55. if (count == 0)
  56. {
  57. return default;
  58. }
  59. if (index == -1)
  60. index = count - 1;
  61. else if (index < 0 || index >= count)
  62. {
  63. Log.Error($"配置表索引越界. Name:{typeof(TTable)} Count:{count} Index:{index}");
  64. return default;
  65. }
  66. return _tables[index];
  67. }
  68. public TTable[] GetAllTables()
  69. {
  70. return _tables;
  71. }
  72. #region 静态方法
  73. public static string TableName => typeof(TTable).Name;
  74. /// <summary>
  75. /// 配置表数量
  76. /// </summary>
  77. public static int Count => GetAll()?.Length ?? 0;
  78. /// <summary>
  79. /// 配置表是否为空
  80. /// </summary>
  81. public static bool IsEmpty => Count == 0;
  82. public static TTable Get(long key)
  83. {
  84. return DatabaseModule.Instance.GetByKey<TTable, TRepository>(key);
  85. }
  86. /// <summary>
  87. /// 配置表第一个数据
  88. /// </summary>
  89. /// <returns></returns>
  90. public static TTable First()
  91. {
  92. return DatabaseModule.Instance.GetByIndex<TTable, TRepository>(0);
  93. }
  94. /// <summary>
  95. /// 配置表最后一个数据
  96. /// </summary>
  97. /// <returns></returns>
  98. public static TTable Last()
  99. {
  100. return DatabaseModule.Instance.GetByIndex<TTable, TRepository>(-1);
  101. }
  102. public static TTable[] GetAll()
  103. {
  104. return DatabaseModule.Instance.GetAll<TTable, TRepository>();
  105. }
  106. public static IAsync LoadAsync()
  107. {
  108. return DatabaseModule.Instance.LoadAsync<TTable, TRepository>(TableName);
  109. }
  110. public static void Unload()
  111. {
  112. DatabaseModule.Instance.Unload(TableName);
  113. }
  114. //public static implicit operator TableRepository<TTable>(TableRepository<ITable> v)
  115. //{
  116. // return v as TableRepository<TTable>;
  117. //}
  118. #endregion
  119. }
  120. }