123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using System.Collections.Generic;
- namespace XGame.Framework.Data
- {
- internal class DataModule : IDataModule, IDisposable
- {
- #region 静态函数
- private static DataModule _instance;
- public static IDataModule Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new DataModule();
- }
- return _instance;
- }
- }
- public static void Dispose()
- {
- (_instance as IDisposable)?.Dispose();
- _instance = null;
- }
- #endregion
- private Dictionary<Type, IData> _dataMap = new Dictionary<Type, IData>();
- TType IDataModule.GetOrAdd<TType>()
- {
- var type = typeof(TType);
- if (!_dataMap.TryGetValue(type, out var data))
- {
- data = Activator.CreateInstance<TType>();
- _dataMap.Add(type, data);
- }
- return data as TType;
- }
- void IDataModule.Remove<TType>()
- {
- var type = typeof(TType);
- if(_dataMap.TryGetValue(type, out var data))
- {
- (data as IDisposable)?.Dispose();
- _dataMap.Remove(type);
- }
- }
- void IDisposable.Dispose()
- {
- foreach (var item in _dataMap.Values)
- {
- (item as IDisposable)?.Dispose();
- }
- _dataMap.Clear();
- }
- }
- }
|