123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Threading;
- namespace Flyinsono.Client.Test
- {
- public interface IClientManager : IDisposable
- {
- }
- public class AppManager
- {
-
-
- private static bool _initialized;
- private static AppManager _instance;
- private readonly Dictionary<Type, IClientManager> _managers = new Dictionary<Type, IClientManager>();
- /// <summary>
- /// The unique instance of app manager
- /// </summary>
- public static AppManager Instance => _instance ?? (_instance = new AppManager());
-
-
- /// <summary>
- /// Register a manager with type and instance
- /// </summary>
- /// <param name="instance">The manager instance</param>
- public void RegisterManager<T>(T instance) where T : IClientManager
- {
- var type = typeof(T);
- if (_managers.ContainsKey(type))
- {
- throw new DuplicateNameException($"Type {type.Name} already exists.");
- }
- _managers[type] = instance;
- Logger.WriteLineInfo($"AppManager Manager {type.Name} registerd");
- }
-
- /// <summary>
- /// Unregister a manager from app manager
- /// </summary>
- void UnRegisterManager<T>() where T : IClientManager
- {
- var type = typeof(T);
- if (!_managers.ContainsKey(type))
- {
- throw new DuplicateNameException($"Type {type} does not exists.");
- }
- _managers[type].Dispose();
- _managers.Remove(type);
- }
- /// <summary>
- /// dispose all managers
- /// </summary>
- public void DisposeAllManagers()
- {
- foreach (var manager in _managers.Reverse())
- {
- try
- {
- Logger.WriteLineInfo($"Disposing - {manager.Key?.Name}");
- manager.Value.Dispose();
- Logger.WriteLineInfo($"Disposed - {manager.Key?.Name}");
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Dispose Manager {manager.Key} failed, ex: {e}");
- }
- }
- }
- /// <summary>
- /// Get Manager by specified type
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public T GetManager<T>() where T : IClientManager
- {
- var type = typeof(T);
- if (_managers.ContainsKey(type))
- {
- return (T)_managers[type];
- }
- throw new NullReferenceException($"Can not find instance for type{type}");
- }
- /// <summary>
- /// Called at client start
- /// </summary>
- public void Initialize()
- {
- try
- {
- Logger.WriteLineInfo("AppManager Initialize begin");
-
- RegisterCommonManagers();
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Initialize Error:{e}");
- }
-
- Logger.WriteLineInfo("AppManager Initialize end");
- }
-
- private void RegisterCommonManagers()
- {
- if (_initialized)
- {
- Logger.WriteLineInfo("Has been Initialized");
- return;
- }
-
- RegisterManager<IClientTestManager>(new ClientTestManager());
-
- Logger.WriteLineInfo("AppManager RegisterPluginCreators Register end");
- _initialized = true;
- }
-
- }
- }
|