using System; using System.Collections.Generic; using XGame.Framework.Asyncs; namespace XGame.Framework.Database { public partial class DatabaseModule : IDatabaseModule, IDisposable { private Dictionary _loadingAsyncMap; private Dictionary _talbeRepoMap; public DatabaseModule() { _loadingAsyncMap = new Dictionary(); _talbeRepoMap = new Dictionary(); } TTable IDatabaseModule.GetByKey(long key) { var tableName = typeof(TTable).Name; if (_talbeRepoMap.TryGetValue(tableName, out var tableRepo)) { return (tableRepo as TRepository).GetByKey(key); } return default; } TTable[] IDatabaseModule.GetAll() { var tableName = typeof(TTable).Name; if (_talbeRepoMap.TryGetValue(tableName, out var tableRepo)) { return (tableRepo as TRepository).GetAllTables(); } return null; } IDatabaseAsync IDatabaseModule.LoadAsync(string tableName) { if (_talbeRepoMap.ContainsKey(tableName)) { Log.Debug($"配置表重复加载. Name:{tableName}"); return null; } if (_loadingAsyncMap.TryGetValue(tableName, out var loadingAsync)) { return loadingAsync as IDatabaseAsync; } var dbAsync = new DatabaseAsync(tableName); _loadingAsyncMap.Add(tableName, dbAsync); dbAsync.On((_) => { _loadingAsyncMap.Remove(tableName); var tableRepo = dbAsync.GetResult(); if (tableRepo != null) { _talbeRepoMap[tableName] = tableRepo; } }); dbAsync.Start(); return dbAsync; } void IDatabaseModule.Unload(string tableName) { if (_talbeRepoMap.ContainsKey(tableName)) { _talbeRepoMap.Remove(tableName); return; } if (_loadingAsyncMap.TryGetValue(tableName, out var loadAsync)) { loadAsync.RemoveAll(); _loadingAsyncMap.Remove(tableName); } } void IDisposable.Dispose() { foreach(var item in _loadingAsyncMap) { item.Value.RemoveAll(); } _loadingAsyncMap.Clear(); foreach(var item in _talbeRepoMap) { (item.Value as IDisposable)?.Dispose(); } _talbeRepoMap.Clear(); } } }