using System; using System.Linq; using Unity; using Unity.Interception; using Unity.Interception.ContainerIntegration; using Unity.Interception.Interceptors.InstanceInterceptors.InterfaceInterception; using Unity.Lifetime; using VCommon.Reflection; namespace VCommon.Ioc { public class IocManager : IIocManager { public IUnityContainer Container { get; } public IocManager() { Container = new UnityContainer(); Container.AddNewExtension(); var allTypes = TypeFinder.AllTypes; foreach (var type in allTypes) RegisterType(type); RegisterInstanceToContainer(this); } private IocManager(IUnityContainer container) { Container = container; } private void RegisterType(Type type) { var interfaces = type.FilterInterfaces( new[] { typeof(IContainerInstanceIocClass), typeof(IContainerInstanceIocClass<>), 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 interceptorClassTarget = iocInterface.GetGenericArguments()[0]; var interceptorClass = typeof(InterceptionBehaviorAdapter<>).MakeGenericType(interceptorClassTarget); foreach (var ri in remainInterfaces) { Container.RegisterType(ri, type, new TransientLifetimeManager() , new Interceptor() , new InterceptionBehavior(interceptorClass)); } } else if (iocDefine == typeof(IContainerInstanceIocClass<>)) { var interceptorClassTarget = iocInterface.GetGenericArguments()[0]; var interceptorClass = typeof(InterceptionBehaviorAdapter<>).MakeGenericType(interceptorClassTarget); foreach (var ri in remainInterfaces) { Container.RegisterType(ri, type, new ContainerControlledLifetimeManager() , new Interceptor() , 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(IContainerInstanceIocClass)) { 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() => Container.Resolve(); public bool IsRegistered() => Container.IsRegistered(); /// 手动注册用以覆盖自动注册 public void RegisterManually() => RegisterType(typeof(T)); /// 将单例实例注册到容器中,使用相同的类型注入 public void RegisterInstanceToContainer(T instance) => Container.RegisterInstance(instance, new ContainerControlledLifetimeManager()); public IIocManager CreateChildren() { var children = new IocManager(Container.CreateChildContainer()); children.RegisterInstanceToContainer(children); return children; } public void Dispose() => Container?.Dispose(); } //TOD: Child Container public interface IIocManager:IDisposable { bool IsRegistered(Type type); bool IsRegistered(); T Resolve(); object Resolve(Type type); bool TryResolve(out T instance) { if (IsRegistered()) { instance = Resolve(); return true; } instance = default(T); return false; } void RegisterManually(); void RegisterInstanceToContainer(T instance); IIocManager CreateChildren(); } }