AppManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. namespace Flyinsono.Client.Test
  7. {
  8. public interface IClientManager : IDisposable
  9. {
  10. }
  11. public class AppManager
  12. {
  13. private static bool _initialized;
  14. private static AppManager _instance;
  15. private readonly Dictionary<Type, IClientManager> _managers = new Dictionary<Type, IClientManager>();
  16. /// <summary>
  17. /// The unique instance of app manager
  18. /// </summary>
  19. public static AppManager Instance => _instance ?? (_instance = new AppManager());
  20. /// <summary>
  21. /// Register a manager with type and instance
  22. /// </summary>
  23. /// <param name="instance">The manager instance</param>
  24. public void RegisterManager<T>(T instance) where T : IClientManager
  25. {
  26. var type = typeof(T);
  27. if (_managers.ContainsKey(type))
  28. {
  29. throw new DuplicateNameException($"Type {type.Name} already exists.");
  30. }
  31. _managers[type] = instance;
  32. Logger.WriteLineInfo($"AppManager Manager {type.Name} registerd");
  33. }
  34. /// <summary>
  35. /// Unregister a manager from app manager
  36. /// </summary>
  37. void UnRegisterManager<T>() where T : IClientManager
  38. {
  39. var type = typeof(T);
  40. if (!_managers.ContainsKey(type))
  41. {
  42. throw new DuplicateNameException($"Type {type} does not exists.");
  43. }
  44. _managers[type].Dispose();
  45. _managers.Remove(type);
  46. }
  47. /// <summary>
  48. /// dispose all managers
  49. /// </summary>
  50. public void DisposeAllManagers()
  51. {
  52. foreach (var manager in _managers.Reverse())
  53. {
  54. try
  55. {
  56. Logger.WriteLineInfo($"Disposing - {manager.Key?.Name}");
  57. manager.Value.Dispose();
  58. Logger.WriteLineInfo($"Disposed - {manager.Key?.Name}");
  59. }
  60. catch (Exception e)
  61. {
  62. Logger.WriteLineError($"Dispose Manager {manager.Key} failed, ex: {e}");
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// Get Manager by specified type
  68. /// </summary>
  69. /// <typeparam name="T"></typeparam>
  70. /// <returns></returns>
  71. public T GetManager<T>() where T : IClientManager
  72. {
  73. var type = typeof(T);
  74. if (_managers.ContainsKey(type))
  75. {
  76. return (T)_managers[type];
  77. }
  78. throw new NullReferenceException($"Can not find instance for type{type}");
  79. }
  80. /// <summary>
  81. /// Called at client start
  82. /// </summary>
  83. public void Initialize()
  84. {
  85. try
  86. {
  87. Logger.WriteLineInfo("AppManager Initialize begin");
  88. RegisterCommonManagers();
  89. }
  90. catch (Exception e)
  91. {
  92. Logger.WriteLineError($"Initialize Error:{e}");
  93. }
  94. Logger.WriteLineInfo("AppManager Initialize end");
  95. }
  96. private void RegisterCommonManagers()
  97. {
  98. if (_initialized)
  99. {
  100. Logger.WriteLineInfo("Has been Initialized");
  101. return;
  102. }
  103. RegisterManager<IClientTestManager>(new ClientTestManager());
  104. Logger.WriteLineInfo("AppManager RegisterPluginCreators Register end");
  105. _initialized = true;
  106. }
  107. }
  108. }