ObjectFactory.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using SKMC.Api.Common.Logger;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace SKMC.Api.Common
  5. {
  6. /// <summary>
  7. /// <p>接口工厂</p>
  8. /// <br>类似IOC容器, 可将接口与实例注册绑定, 在需要调用接口时获取实例</br>
  9. /// <br>Resolve获取单例, Create创建新实例</br>
  10. /// </summary>
  11. public class ObjectFactory
  12. {
  13. private static readonly ILogger log = LogFactory.Get();
  14. private static readonly Dictionary<Type, Dictionary<string, object>> container = new Dictionary<Type, Dictionary<string, object>>();
  15. /// <summary>
  16. /// 注册接口实例
  17. /// </summary>
  18. /// <typeparam name="T"></typeparam>
  19. /// <param name="obj"></param>
  20. /// <param name="name"></param>
  21. public static void Register<T>(object obj, string name = "default")
  22. {
  23. Type type = typeof(T);
  24. if (!container.ContainsKey(type))
  25. {
  26. container[type] = new Dictionary<string, object>();
  27. }
  28. //else
  29. //{
  30. // throw new SystemException($"Register Failed, Cannot Register SameType: {type.Name}");
  31. //}
  32. container[type][name] = obj;
  33. log.Debug($"Register {type.Name} success");
  34. }
  35. /// <summary>
  36. /// 反注册接口实例, 重复注册前需要反注册
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  39. public static void UnRegister<T>()
  40. {
  41. Type type = typeof(T);
  42. if (container.ContainsKey(type))
  43. {
  44. bool result = container.Remove(type);
  45. log.Debug($"UnRegister Type: {type.Name}: {result}");
  46. }
  47. }
  48. /// <summary>
  49. /// 解析接口实例(单例)
  50. /// </summary>
  51. /// <typeparam name="T"></typeparam>
  52. /// <param name="name"></param>
  53. /// <returns></returns>
  54. public static T Resolve<T>(string name = "default")
  55. {
  56. Type type = typeof(T);
  57. if (container.ContainsKey(type) && container[type].ContainsKey(name))
  58. {
  59. object obj = (T)container[type][name];
  60. //log.Debug($"Create {type.Name} success, obj: {obj.GetHashCode()}");
  61. return (T)obj;
  62. }
  63. throw new SystemException($"Resolve Faild, No object for [{type}] with name [{name}] found.");
  64. }
  65. /// <summary>
  66. /// 创建接口实例(多例)
  67. /// </summary>
  68. /// <typeparam name="T"></typeparam>
  69. /// <param name="name"></param>
  70. /// <returns></returns>
  71. public static T Create<T>(string name = "default")
  72. {
  73. Type type = typeof(T);
  74. if (container.ContainsKey(type) && container[type].ContainsKey(name))
  75. {
  76. object objectNow = container[type][name];
  77. object objectNew = Activator.CreateInstance(objectNow.GetType());
  78. //log.Debug($"Create {type.Name} success, obj: {objectNew.GetHashCode()}");
  79. return (T)objectNew;
  80. }
  81. throw new SystemException($"Create Faild, No object for [{type}] with name [{name}] found.");
  82. }
  83. }
  84. }