123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using System.Linq;
- using Unity;
- using Unity.Interception.ContainerIntegration;
- using Unity.Interception.Interceptors.InstanceInterceptors.InterfaceInterception;
- using Unity.Lifetime;
- using VCommon.Reflection;
- namespace VCommon.Ioc
- {
- public class IocManager
- {
- public static IocManager Instance { get; } = new IocManager();
- public UnityContainer Container { get; }
- private IocManager()
- {
- Container = new UnityContainer();
- var allTypes = TypeFinder.AllTypes;
- Container.AddNewExtension<Interception>();
- foreach (var type in allTypes) RegisterType(type);
- }
- private void RegisterType(Type type)
- {
- var interfaces = type.FilterInterfaces(
- new[]
- {
- typeof(ISingletonIocClass),
- typeof(ISingletonIocClass<>),
- typeof(ITransientIocClass),
- typeof(ITransientIocClass<>)
- }
- , out var remainInterfaces
- );
- if (interfaces.Count == 0) return;
- if (interfaces.Count > 1) throw new VIocException($"Ioc接口只能有一个: {type.FullName}");
- var keyValuePair = interfaces.First();
- var list = keyValuePair.Value;
- if (list.Length != 1) throw new VIocException($"Ioc接口只能有一个: {type.FullName}");
- var iocDefine = keyValuePair.Key;
- var iocInterface = list[0];
- if (iocDefine == typeof(ITransientIocClass<>))
- {
- var interceptorClass = iocInterface.GetGenericArguments()[0];
- foreach (var ri in remainInterfaces)
- {
- Container.RegisterType(ri, type, new TransientLifetimeManager()
- , new Interceptor<InterfaceInterceptor>()
- , new InterceptionBehavior(interceptorClass));
- }
- }
- else if (iocDefine == typeof(ISingletonIocClass<>))
- {
- var interceptorClass = iocInterface.GetGenericArguments()[0];
- foreach (var ri in remainInterfaces)
- {
- Container.RegisterType(ri, type, new ContainerControlledLifetimeManager()
- , new Interceptor<InterfaceInterceptor>()
- , new InterceptionBehavior(interceptorClass));
- }
- }
- else if (iocDefine == typeof(ITransientIocClass))
- {
- Container.RegisterType(type, type, new TransientLifetimeManager());
- foreach (var ri in remainInterfaces) Container.RegisterType(ri, type, new TransientLifetimeManager());
- }
- else if (iocDefine == typeof(ISingletonIocClass))
- {
- Container.RegisterType(type, type, new ContainerControlledLifetimeManager());
- foreach (var ri in remainInterfaces) Container.RegisterType(ri, type, new ContainerControlledLifetimeManager());
- }
- else
- {
- throw new VIocException($"这是什么Ioc接口? {iocDefine.FullName}");
- }
- }
- public bool IsRegistered(Type type) => Container.IsRegistered(type);
- public object Resolve(Type type) => Container.Resolve(type, null);
- public T Resolve<T>() => Container.Resolve<T>();
- public bool IsRegistered<T>() => IsRegistered(typeof(T));
- public bool TryResolve<T>(out T instance)
- {
- if (IsRegistered<T>())
- {
- instance = Resolve<T>();
- return true;
- }
- instance = default(T);
- return false;
- }
- public bool TryResolve(Type type, out object instance)
- {
- if (IsRegistered(type))
- {
- instance = Resolve(type);
- return true;
- }
- instance = null;
- return false;
- }
- /// <summary> 手动注册用以覆盖自动注册 </summary>
- public void RegisterManually<T>() => RegisterType(typeof(T));
- /// <summary> 将单例实例注册到容器中,使用相同的类型注入 </summary>
- public void RegisterSingletonInstance<T>(T instance) => Container.RegisterInstance(instance, new SingletonLifetimeManager());
- }
- }
|