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)); }
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)); }
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>()); }
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); }
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)); }
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()); }
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); } }
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; } }
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); }
public abstract object Constructor(IUnityAddonSP serviceProvider, Type type, string name);
public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name) { return(Instance); }
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)); }
public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name) { return(serviceProvider.GetRequiredService <BeanFactory>() .Construct(type, serviceProvider)); }
private object InvokeStrategy <TAttribute>(Func <Type, TAttribute, IUnityAddonSP, object> strategy, Type type, TAttribute attr, IUnityAddonSP sp) { return(strategy(type, attr, sp)); }
public Foo(IUnityAddonSP sp) { ServiceProvider = sp; }
public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name) { return(Factory(serviceProvider, type, name)); }
public override object Constructor(IUnityAddonSP serviceProvider, Type type, string name) { return(serviceProvider.GetRequiredService <BeanFactory>() .ConstructClassProxy(type, new IInterceptor[] { serviceProvider.GetRequiredService <BeanMethodInterceptor>() }, serviceProvider)); }