IocManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Linq;
  3. using Unity;
  4. using Unity.Interception.ContainerIntegration;
  5. using Unity.Interception.Interceptors.InstanceInterceptors.InterfaceInterception;
  6. using Unity.Lifetime;
  7. using VCommon.Reflection;
  8. namespace VCommon.Ioc
  9. {
  10. public class IocManager
  11. {
  12. public static IocManager Instance { get; } = new IocManager();
  13. public UnityContainer Container { get; }
  14. private IocManager()
  15. {
  16. Container = new UnityContainer();
  17. var allTypes = TypeFinder.AllTypes;
  18. Container.AddNewExtension<Interception>();
  19. foreach (var type in allTypes) RegisterType(type);
  20. }
  21. private void RegisterType(Type type)
  22. {
  23. var interfaces = type.FilterInterfaces(
  24. new[]
  25. {
  26. typeof(ISingletonIocClass),
  27. typeof(ISingletonIocClass<>),
  28. typeof(ITransientIocClass),
  29. typeof(ITransientIocClass<>)
  30. }
  31. , out var remainInterfaces
  32. );
  33. if (interfaces.Count == 0) return;
  34. if (interfaces.Count > 1) throw new VIocException($"Ioc接口只能有一个: {type.FullName}");
  35. var keyValuePair = interfaces.First();
  36. var list = keyValuePair.Value;
  37. if (list.Length != 1) throw new VIocException($"Ioc接口只能有一个: {type.FullName}");
  38. var iocDefine = keyValuePair.Key;
  39. var iocInterface = list[0];
  40. if (iocDefine == typeof(ITransientIocClass<>))
  41. {
  42. var interceptorClass = iocInterface.GetGenericArguments()[0];
  43. foreach (var ri in remainInterfaces)
  44. {
  45. Container.RegisterType(ri, type, new TransientLifetimeManager()
  46. , new Interceptor<InterfaceInterceptor>()
  47. , new InterceptionBehavior(interceptorClass));
  48. }
  49. }
  50. else if (iocDefine == typeof(ISingletonIocClass<>))
  51. {
  52. var interceptorClass = iocInterface.GetGenericArguments()[0];
  53. foreach (var ri in remainInterfaces)
  54. {
  55. Container.RegisterType(ri, type, new ContainerControlledLifetimeManager()
  56. , new Interceptor<InterfaceInterceptor>()
  57. , new InterceptionBehavior(interceptorClass));
  58. }
  59. }
  60. else if (iocDefine == typeof(ITransientIocClass))
  61. {
  62. Container.RegisterType(type, type, new TransientLifetimeManager());
  63. foreach (var ri in remainInterfaces) Container.RegisterType(ri, type, new TransientLifetimeManager());
  64. }
  65. else if (iocDefine == typeof(ISingletonIocClass))
  66. {
  67. Container.RegisterType(type, type, new ContainerControlledLifetimeManager());
  68. foreach (var ri in remainInterfaces) Container.RegisterType(ri, type, new ContainerControlledLifetimeManager());
  69. }
  70. else
  71. {
  72. throw new VIocException($"这是什么Ioc接口? {iocDefine.FullName}");
  73. }
  74. }
  75. public bool IsRegistered(Type type) => Container.IsRegistered(type);
  76. public object Resolve(Type type) => Container.Resolve(type, null);
  77. public T Resolve<T>() => Container.Resolve<T>();
  78. public bool IsRegistered<T>() => IsRegistered(typeof(T));
  79. public bool TryResolve<T>(out T instance)
  80. {
  81. if (IsRegistered<T>())
  82. {
  83. instance = Resolve<T>();
  84. return true;
  85. }
  86. instance = default(T);
  87. return false;
  88. }
  89. public bool TryResolve(Type type, out object instance)
  90. {
  91. if (IsRegistered(type))
  92. {
  93. instance = Resolve(type);
  94. return true;
  95. }
  96. instance = null;
  97. return false;
  98. }
  99. /// <summary> 手动注册用以覆盖自动注册 </summary>
  100. public void RegisterManually<T>() => RegisterType(typeof(T));
  101. /// <summary> 将单例实例注册到容器中,使用相同的类型注入 </summary>
  102. public void RegisterSingletonInstance<T>(T instance) => Container.RegisterInstance(instance, new SingletonLifetimeManager());
  103. }
  104. }