DatabaseManager.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using FISSDKDemoV2.Database.IRespository;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace FISSDKDemoV2.Manager
  5. {
  6. internal class DataBaseManager
  7. {
  8. private static Dictionary<Type, IDatabaseService> _services = new Dictionary<Type, IDatabaseService>();
  9. /// <summary>
  10. /// Register a service into the container.
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. /// <param name="service"></param>
  14. /// <exception cref="InvalidOperationException"></exception>
  15. public static void RegisterService<T>(IDatabaseService service)
  16. {
  17. var type = typeof(T);
  18. if (_services.ContainsKey(type))
  19. {
  20. throw new InvalidOperationException($"Key:{type.Name} already exists.");
  21. }
  22. _services.Add(type, service);
  23. }
  24. /// <summary>
  25. /// Get the service from the container.
  26. /// </summary>
  27. /// <typeparam name="T"></typeparam>
  28. /// <returns></returns>
  29. /// <exception cref="KeyNotFoundException"></exception>
  30. public static T GetService<T>()
  31. {
  32. var type = typeof(T);
  33. if (_services.ContainsKey(type))
  34. {
  35. return (T)_services[type];
  36. }
  37. throw new KeyNotFoundException($"Can not find the key:{type.Name}");
  38. }
  39. }
  40. }