IocManager.cs 5.1 KB

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