Esempio n. 1
0
 /// <summary>
 /// 分析
 /// </summary>
 /// <param name="serviceProvider"></param>
 /// <param name="instanceScopeModel"></param>
 private static void ResolveDependencyTree(IServiceProvider serviceProvider, InstanceScopeModel instanceScopeModel)
 {
     foreach (var memberInfo in instanceScopeModel.Instance.GetType().GetFullMembers())
     {
         var customeAttribute = memberInfo.GetCustomAttribute(autowiredAttributeType, false);
         var type             = ((AutowiredAttribute)customeAttribute).RealType ?? memberInfo.GetRealType();
         var value            = GetInstance(instanceScopeModel, type);
         //如果依赖树能找到,则说明此处含有循环依赖,从依赖树还原
         //从parent instance 还原
         if (value != null)
         {
             memberInfo.SetValue(instanceScopeModel.Instance, value);
             continue;
         }
         //从容器拿到Instance
         value = serviceProvider.GetService(type);
         if (value == null)
         {
             throw new UnableResolveDependencyException($"Unable to resolve dependency {memberInfo.GetRealType().FullName}");
         }
         //将Instance赋值给属性
         memberInfo.SetValue(instanceScopeModel.Instance, value);
         //构建下一个节点
         var nextInstanceScopeModel = new InstanceScopeModel
         {
             Instance            = value,
             ParentInstanceScope = instanceScopeModel
         };
         //递归注入的属性是否有其它依赖
         ResolveDependencyTree(serviceProvider, nextInstanceScopeModel);
     }
 }
Esempio n. 2
0
 /// <summary>
 /// 递归查找父节点
 /// </summary>
 /// <param name="instanceScopeModel"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 private static object GetInstance(InstanceScopeModel instanceScopeModel, Type type)
 {
     if (instanceScopeModel.Instance.GetType() == type)
     {
         return(instanceScopeModel.Instance);
     }
     if (instanceScopeModel.ParentInstanceScope == null)
     {
         return(null);
     }
     return(GetInstance(instanceScopeModel.ParentInstanceScope, type));
 }
        /// <summary>
        /// 分析
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="instanceScopeModel"></param>
        private static void ResolveDependencyTree(IServiceProvider serviceProvider, InstanceScopeModel instanceScopeModel)
        {
            foreach (var memberInfo in instanceScopeModel.Instance.GetType().GetFullMembers())
            {
                var customeAttribute = memberInfo.GetCustomAttribute(autowiredAttributeType, false);
                var memberType       = memberInfo.GetRealType();
                var realType         = ((AutowiredAttribute)customeAttribute).RealType;
                var instance         = GetInstance(instanceScopeModel, realType ?? memberType);
                //如果依赖树能找到,则说明此处含有循环依赖,从依赖树还原
                //从parent instance 还原
                if (instance != null)
                {
                    memberInfo.SetValue(instanceScopeModel.Instance, instance);
                    continue;
                }
                if (realType == null)
                {
                    var implements = serviceProvider.GetServices(memberType);
                    if (implements == null)
                    {
                        throw new UnableResolveDependencyException($"Unable to resolve dependency {memberType.FullName}");
                    }
                    if (implements.Count() > 1)
                    {
                        throw new UnableResolveDependencyException($"Interfaces with multiple implementations, use [Autowired(typeof(ImplementClass))] to explicit resolve");
                    }
                    instance = implements.FirstOrDefault();
                }
                else
                {
                    //从容器拿到Instance
                    instance = serviceProvider.GetServices(memberType)?.FirstOrDefault(i => i.GetType() == realType);
                }

                if (instance == null)
                {
                    throw new UnableResolveDependencyException($"Unable to resolve dependency {memberType.FullName}");
                }
                //将Instance赋值给属性
                memberInfo.SetValue(instanceScopeModel.Instance, instance);
                //构建下一个节点
                var nextInstanceScopeModel = new InstanceScopeModel
                {
                    Instance            = instance,
                    ParentInstanceScope = instanceScopeModel
                };
                //递归注入的属性是否有其它依赖
                ResolveDependencyTree(serviceProvider, nextInstanceScopeModel);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 分析
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="instanceScopeModel"></param>
        private static void ResolveDependencyTree(IServiceProvider serviceProvider, InstanceScopeModel instanceScopeModel)
        {
            foreach (var memberInfo in instanceScopeModel.Instance.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                //非属性和字段
                if (memberInfo.MemberType != MemberTypes.Field && memberInfo.MemberType != MemberTypes.Property)
                {
                    continue;
                }
                //判断当前属性是否具有DependencyInjectionAttribute特性
                var customeAttribute = memberInfo.GetCustomAttribute(typeof(AutowiredAttribute), false);
                if (customeAttribute == null)
                {
                    continue;
                }

                var type  = ((AutowiredAttribute)customeAttribute).RealType ?? memberInfo.GetRealType();
                var value = GetInstance(instanceScopeModel, type);
                //从parent instance 还原
                if (value != null)
                {
                    memberInfo.SetValue(instanceScopeModel.Instance, value);
                    continue;
                }
                //从容器拿到Instance
                value = serviceProvider.GetService(type);
                if (value == null)
                {
                    throw new UnableResolveDependencyException($"Unable to resolve dependency {memberInfo.GetRealType().FullName}");
                }
                //将Instance赋值给属性
                memberInfo.SetValue(instanceScopeModel.Instance, value);
                //构建下一个节点
                var nextInstanceScopeModel = new InstanceScopeModel {
                    Instance            = value,
                    ParentInstanceScope = instanceScopeModel
                };
                //递归注入的属性是否有其它依赖
                ResolveDependencyTree(serviceProvider, nextInstanceScopeModel);
            }
        }
        /// <summary>
        /// 分析
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="instanceScopeModel"></param>
        private static void AnalysisDependencyInjection(IServiceProvider serviceProvider, InstanceScopeModel instanceScopeModel)
        {
            foreach (var propertity in instanceScopeModel.Instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                //判断当前属性是否具有DependencyInjectionAttribute特性
                var customeAttribute = propertity.GetCustomAttribute(typeof(AutowiredAttribute), false);
                if (customeAttribute == null)
                {
                    continue;
                }

                var type  = ((AutowiredAttribute)customeAttribute).RealType ?? propertity.PropertyType;
                var value = GetInstance(instanceScopeModel, type);
                //从parent instance 还原
                if (value != null)
                {
                    propertity.SetValue(instanceScopeModel.Instance, value);
                    continue;
                }
                //从容器拿到Instance
                value = serviceProvider.GetService(type);
                if (value == null)
                {
                    throw new UnableResolveDependencyException($"Unable to resolve dependency {propertity.PropertyType.FullName}");
                }
                //将Instance赋值给属性
                propertity.SetValue(instanceScopeModel.Instance, value);
                //构建下一个节点
                var nextInstanceScopeModel = new InstanceScopeModel {
                    Instance            = value,
                    ParentInstanceScope = instanceScopeModel
                };
                //递归注入的属性是否有其它依赖
                AnalysisDependencyInjection(serviceProvider, nextInstanceScopeModel);
            }
        }
        /// <summary>
        /// 分析
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="instanceScopeModel"></param>
        private static void ResolveDependencyTree(IServiceProvider serviceProvider, InstanceScopeModel instanceScopeModel)
        {
            foreach (var memberInfo in instanceScopeModel.Instance.GetType().GetFullMembers())
            {
                var customeAttribute = memberInfo.GetCustomAttribute(autowiredAttributeType, false);
                var memberType       = memberInfo.GetRealType();
                var realType         = ((AutowiredAttribute)customeAttribute).RealType;
                var instance         = GetInstance(instanceScopeModel, realType ?? memberType);
                //如果依赖树能找到,则说明此处含有循环依赖,从依赖树还原
                //从parent instance 还原
                if (instance != null)
                {
                    memberInfo.SetValue(instanceScopeModel.Instance, instance);
                    continue;
                }

                bool memberIsEnumerable = typeof(IEnumerable).IsAssignableFrom(memberType) && memberType.IsGenericType;
                //解析IEnumberable<T>服务集合
                if (memberIsEnumerable)
                {
                    Type elType     = memberType.GetGenericArguments()[0];
                    var  implements = serviceProvider.GetServices(elType);
                    if (realType == null)
                    {
                        instance = implements;
                    }
                    else
                    {
                        instance = implements?.Where(i => i.GetType() == realType);
                    }
                }
                else //解析单个服务
                {
                    instance = serviceProvider.GetService(memberType);
                }

                if (instance == null)
                {
                    throw new UnableResolveDependencyException($"Unable to resolve dependency {memberType.FullName}");
                }
                //将Instance赋值给属性
                memberInfo.SetValue(instanceScopeModel.Instance, instance);

                //本层可能注入服务集合,那么下层需要注入的是集合内的实例。
                IEnumerable elementInstances = memberIsEnumerable ? (IEnumerable)instance : new ArrayList()
                {
                    instance
                };
                foreach (var ins in elementInstances)
                {
                    //构建下一个节点
                    var nextInstanceScopeModel = new InstanceScopeModel
                    {
                        Instance            = ins,
                        ParentInstanceScope = instanceScopeModel
                    };
                    //递归注入的属性是否有其它依赖
                    ResolveDependencyTree(serviceProvider, nextInstanceScopeModel);
                }
            }
        }