示例#1
0
        public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name)
        {
            var impl = ImplType.IsGenericType && ImplType.ContainsGenericParameters ?
                       ImplType.MakeGenericType(type.GetGenericArguments()) : ImplType;

            return(serviceProvider.GetRequiredService <BeanFactory>().Construct(impl, serviceProvider));
        }
示例#2
0
        public void InjectDependency(PropertyInfo prop, object obj, IUnityAddonSP sp)
        {
            if (prop.SetMethod == null)
            {
                return;
            }

            try
            {
                var dep = DependencyResolver.Resolve(prop.PropertyType, prop.GetCustomAttributes(false).Cast <Attribute>(), sp);

                // must add null check, else something will be wrong.
                if (dep != null)
                {
                    prop.SetMethod.Invoke(obj, new[] { dep });
                }
            }
            catch (NoSuchBeanDefinitionException ex)
            {
                if (ex.InnerException == null)
                {
                    throw new NoSuchBeanDefinitionException($"Property {prop.Name} in {prop.DeclaringType} required a bean of type '{prop.PropertyType}' that could not be found.", ex);
                }
                else
                {
                    throw;
                }
            }
        }
        public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name)
        {
            var config    = serviceProvider.GetRequiredService(ConfigType);
            var paramFill = serviceProvider.GetRequiredService <ParameterFill>();

            // enter into the interceptor, construct the bean inside the interceptor
            return(Method.Invoke(config, paramFill.FillAllParamaters(Method, serviceProvider)));
        }
        public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name)
        {
            var invocation = _parentBeanDef.Invocation;

            invocation.Proceed();

            return(new MethodFactoryValue(invocation.ReturnValue));
        }
示例#5
0
        public object FillAllProperties(Type type, object obj, IUnityAddonSP sp)
        {
            foreach (var prop in SelectAllProperties(type))
            {
                InjectDependency(prop, obj, sp);
            }

            return(obj);
        }
        public void CoreContextResolve()
        {
            IUnityAddonSP sp = null;

            using (var scope = Sp.CreateScope())
            {
                sp = scope.ServiceProvider.GetRequiredService <IUnityAddonSP>();
                // dispose scope should not dispose service provider
            }

            Assert.NotNull(sp.GetRequiredService <IUnityAddonSP>());
        }
示例#7
0
        public IUnityContainer Build(IUnityAddonSP sp)
        {
            IUnityContainer container = new UnityContainer();

            container.RegisterType <ValueProvider>(new SingletonLifetimeManager());
            container.RegisterFactory <ConfigBracketParser>(c => new ConfigBracketParser(sp.GetService <IConfiguration>()), new SingletonLifetimeManager());
            container.RegisterType <DependencyResolverOption>(new SingletonLifetimeManager())
            .Resolve <DependencyResolverOption>().AddResolveStrategy <ValueAttribute>((type, attr, sp) =>
                                                                                      sp.GetRequiredService <ValueProvider>().GetValue(type, attr.Value));

            return(container);
        }
示例#8
0
        public object Construct(Type type, IUnityAddonSP sp)
        {
            if (type.IsInterface)
            {
                return(ProxyGenerator.CreateInterfaceProxyWithoutTarget(type));
            }

            var ctor = CtorResolver.ChooseConstuctor(type, sp);
            var bean = ctor.Invoke(ParameterFill.FillAllParamaters(ctor, sp).ToArray());

            PropertyFill.FillAllProperties(type, bean, sp);

            return(PostConstruct(type, bean));
        }
示例#9
0
        public ConstructorInfo ChooseConstuctor(Type type, IUnityAddonSP sp)
        {
            var ctors = type.GetConstructors()
                        .Where(ctor => ctor.GetParameters().All(p => sp.CanResolve(p.ParameterType) || ParameterFill.CanResolve(p)));

            if (ctors.Count() == 0)
            {
                throw new BeanCreationException($"Fail to satisfy any of these constructors{string.Join("", type.GetConstructors().Select(ctor => "\r\n- " + ctor.ToString()))}");
            }

            var maxFill       = ctors.Max(ctor => ctor.GetParameters().Count());
            var selectedCtors = ctors.Where(ctor => ctor.GetParameters().Count() == maxFill);

            if (selectedCtors.Count() > 1)
            {
                throw new BeanCreationException($"Ambiguous constructors are found{string.Join("", selectedCtors.Select(ctor => "\r\n- " + ctor.ToString()))}");
            }

            return(selectedCtors.Single());
        }
示例#10
0
        public object GetDependency(ParameterInfo param, IUnityAddonSP sp)
        {
            var attrs = param.GetCustomAttributes(false).Cast <Attribute>();

            try
            {
                if (attrs.Count() == 0)
                {
                    return(sp.GetRequiredService(param.ParameterType));
                }

                return(DependencyResolver.Resolve(param.ParameterType, attrs, sp));
            }
            catch (NoSuchBeanDefinitionException ex)
            {
                throw new NoSuchBeanDefinitionException(
                          $"Parameter {param.Position} of Constructor in {param.Member} required a bean of type '{param.ParameterType}' that could not be found.",
                          ex);
            }
        }
示例#11
0
        public object Resolve(Type resolveType, IEnumerable <Attribute> attributes, IUnityAddonSP sp)
        {
            try
            {
                foreach (var attribute in attributes)
                {
                    var attrType = attribute.GetType();

                    if (_resolveStrategies.ContainsKey(attrType))
                    {
                        return(InvokeStrategyMethod.MakeGenericMethod(attrType).Invoke(this, new object[] { _resolveStrategies[attrType], resolveType, attribute, sp }));
                    }
                }

                return(null);
            }
            catch (TargetInvocationException ex)
            {
                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();

                throw;
            }
        }
示例#12
0
 public object[] FillAllParamaters(MethodBase method, IUnityAddonSP sp)
 {
     return(method.GetParameters().Select(param => GetDependency(param, sp)).ToArray());
 }
 public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name)
 {
     return(Descriptor.ImplementationInstance);
 }
示例#14
0
 public abstract object Constructor(IUnityAddonSP serviceProvider, Type type, string name);
 public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name)
 {
     return(Instance);
 }
示例#16
0
        public object ConstructClassProxy(Type type, IEnumerable <IInterceptor> interceptors, IUnityAddonSP sp)
        {
            var proxyBean = ProxyGenerator.CreateClassProxy(
                type,
                ParameterFill.FillAllParamaters(CtorResolver.ChooseConstuctor(type, sp), sp),
                interceptors.ToArray());

            PropertyFill.FillAllProperties(type, proxyBean, sp);

            return(PostConstruct(type, proxyBean));
        }
示例#17
0
 public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name)
 {
     return(serviceProvider.GetRequiredService <BeanFactory>()
            .Construct(type, serviceProvider));
 }
示例#18
0
 private object InvokeStrategy <TAttribute>(Func <Type, TAttribute, IUnityAddonSP, object> strategy, Type type, TAttribute attr, IUnityAddonSP sp)
 {
     return(strategy(type, attr, sp));
 }
示例#19
0
 public Foo(IUnityAddonSP sp)
 {
     ServiceProvider = sp;
 }
 public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name)
 {
     return(Factory(serviceProvider, type, name));
 }
示例#21
0
 public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name)
 {
     return(serviceProvider.GetRequiredService <BeanFactory>()
            .ConstructClassProxy(type, new IInterceptor[] { serviceProvider.GetRequiredService <BeanMethodInterceptor>() }, serviceProvider));
 }