DAL.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class DAL : Singleton<DAL>
  6. {
  7. private Dictionary<Type, Proxy> m_map;
  8. public DAL()
  9. {
  10. m_map = new Dictionary<Type, Proxy>();
  11. }
  12. public Proxy Get(Type type)
  13. {
  14. Proxy proxy;
  15. if (!m_map.TryGetValue(type, out proxy))
  16. {
  17. proxy = (DataProxy)Activator.CreateInstance(type, true);
  18. m_map.Add(type, proxy);
  19. proxy.OnRegister();
  20. }
  21. return proxy;
  22. }
  23. public T Get<T>() where T : DataProxy, new()
  24. {
  25. return Get(typeof(T)) as T;
  26. }
  27. public bool Remove(Type type)
  28. {
  29. Proxy proxy;
  30. if(m_map.TryGetValue(type, out proxy))
  31. {
  32. proxy.OnRemove();
  33. return m_map.Remove(type);
  34. }
  35. return false;
  36. }
  37. public bool Remove<T>() where T : DataProxy
  38. {
  39. return Remove(typeof(T));
  40. }
  41. public void Clear()
  42. {
  43. foreach (var item in m_map)
  44. item.Value.OnRemove();
  45. m_map.Clear();
  46. }
  47. }