Пример #1
0
        public Expression[] BuildResolutionExpressions(IContainerContext containerContext, ResolutionInfo resolutionInfo, TypeInformation typeInformation)
        {
            var registrations = containerContext.RegistrationRepository.GetRegistrationsOrDefault(typeInformation.Type)?.CastToArray();

            if (registrations == null)
            {
                return(this.resolverSelector.GetResolverExpressions(containerContext, typeInformation, resolutionInfo));
            }

            var lenght      = registrations.Length;
            var expressions = new Expression[lenght];

            for (var i = 0; i < lenght; i++)
            {
                expressions[i] = registrations[i].Value.GetExpression(resolutionInfo, typeInformation.Type);
            }

            return(expressions);
        }
Пример #2
0
        public Expression BuildResolutionExpression(IContainerContext containerContext, ResolutionContext resolutionContext, TypeInformation typeInformation,
                                                    InjectionParameter[] injectionParameters)
        {
            if (typeInformation.Type == Constants.ResolverType)
            {
                return(resolutionContext.CurrentScopeParameter.ConvertTo(Constants.ResolverType));
            }

            if (resolutionContext.ParameterExpressions.Length > 0)
            {
                var length = resolutionContext.ParameterExpressions.Length;
                for (var i = length; i-- > 0;)
                {
                    var parameters = resolutionContext.ParameterExpressions[i].WhereOrDefault(p => p.Value.Type == typeInformation.Type ||
                                                                                              p.Value.Type.Implements(typeInformation.Type));

                    if (parameters == null)
                    {
                        continue;
                    }
                    var selected = parameters.Repository.FirstOrDefault(parameter => !parameter.Key) ?? parameters.Repository.Last();
                    selected.Key = true;
                    return(selected.Value);
                }
            }

            var matchingParam = injectionParameters?.FirstOrDefault(param => param.Name == typeInformation.ParameterName);

            if (matchingParam != null)
            {
                return(matchingParam.Value.AsConstant());
            }

            var exprOverride = resolutionContext.GetExpressionOverrideOrDefault(typeInformation.Type);

            if (exprOverride != null)
            {
                return(exprOverride);
            }

            var registration = containerContext.RegistrationRepository.GetRegistrationOrDefault(typeInformation, resolutionContext);

            return(registration != null?registration.GetExpression(resolutionContext.ChildContext ?? containerContext, resolutionContext, typeInformation.Type) :
                       this.resolverSelector.GetResolverExpression(containerContext, typeInformation, resolutionContext));
        }
Пример #3
0
        public Expression BuildResolutionExpression(IContainerContext containerContext, ResolutionInfo resolutionInfo, TypeInformation typeInformation,
                                                    InjectionParameter[] injectionParameters)
        {
            if (typeInformation.Type == Constants.ResolverType)
            {
                return(Expression.Convert(Constants.ScopeExpression, Constants.ResolverType));
            }

            if (resolutionInfo.ParameterExpressions != null && resolutionInfo.ParameterExpressions.Any(p => p.Type == typeInformation.Type || p.Type.Implements(typeInformation.Type)))
            {
                return(resolutionInfo.ParameterExpressions.Last(p => p.Type == typeInformation.Type || p.Type.Implements(typeInformation.Type)));
            }

            var matchingParam = injectionParameters?.FirstOrDefault(param => param.Name == typeInformation.ParameterName);

            if (matchingParam != null)
            {
                return(Expression.Constant(matchingParam.Value));
            }

            var exprOverride = resolutionInfo.GetExpressionOverrideOrDefault(typeInformation.Type);

            if (exprOverride != null)
            {
                return(exprOverride);
            }

            var registration = containerContext.RegistrationRepository.GetRegistrationOrDefault(typeInformation, true);

            return(registration != null?registration.GetExpression(resolutionInfo, typeInformation.Type) :
                       this.resolverSelector.GetResolverExpression(containerContext, typeInformation, resolutionInfo));
        }
Пример #4
0
        public IEnumerable <Expression> BuildExpressionsForEnumerableRequest(ResolutionContext resolutionContext, TypeInformation typeInformation)
        {
            var registrations = resolutionContext
                                .CurrentContainerContext
                                .RegistrationRepository
                                .GetRegistrationsOrDefault(typeInformation, resolutionContext);

            if (registrations == null)
            {
                return(this.BuildAllResolverExpressionsUsingResolvers(typeInformation, resolutionContext));
            }

            return(registrations.Select(reg =>
            {
                var decorators = resolutionContext.RemainingDecorators.GetOrDefaultByRef(typeInformation.Type);
                if (decorators == null || decorators.Length == 0)
                {
                    return this.BuildExpressionForRegistration(reg, resolutionContext, typeInformation);
                }

                decorators.ReplaceBack(reg);
                return this.BuildExpressionForDecorator(decorators.Front(),
                                                        resolutionContext.BeginDecoratingContext(typeInformation.Type, decorators),
                                                        typeInformation.Type, decorators);
            }));
        }
Пример #5
0
        public Expression[] GetResolverExpressions(IContainerContext containerContext, TypeInformation typeInfo, ResolutionContext resolutionContext)
        {
            for (var i = 0; i < this.multiServiceResolverRepository.Length; i++)
            {
                var item = this.multiServiceResolverRepository.Get(i);
                if (item.CanUseForResolution(containerContext, typeInfo, resolutionContext))
                {
                    return(item.GetExpressions(containerContext, typeInfo, resolutionContext));
                }
            }

            return(this.parentContainerResolver.CanUseForResolution(containerContext, typeInfo, resolutionContext) ? this.parentContainerResolver.GetExpressions(containerContext, typeInfo, resolutionContext) : null);
        }
Пример #6
0
        public Expression BuildExpressionForType(ResolutionContext resolutionContext, TypeInformation typeInformation)
        {
            if (typeInformation.Type == Constants.ResolverType)
            {
                return(resolutionContext.CurrentScopeParameter);
            }

            if (!resolutionContext.IsTopRequest)
            {
                if (resolutionContext.ParameterExpressions.Length > 0)
                {
                    var type   = typeInformation.Type;
                    var length = resolutionContext.ParameterExpressions.Length;
                    for (var i = length; i-- > 0;)
                    {
                        var parameters = resolutionContext.ParameterExpressions[i]
                                         .WhereOrDefault(p => p.I2.Type == type ||
                                                         p.I2.Type.Implements(type));

                        if (parameters == null)
                        {
                            continue;
                        }
                        var selected = parameters.FirstOrDefault(parameter => !parameter.I1) ?? parameters[parameters.Length - 1];
                        selected.I1 = true;
                        return(selected.I2);
                    }
                }

                var decorators = resolutionContext.RemainingDecorators.GetOrDefaultByRef(typeInformation.Type);
                if (decorators != null && decorators.Length > 0)
                {
                    return(this.BuildExpressionForDecorator(decorators.Front(),
                                                            resolutionContext.BeginDecoratingContext(typeInformation.Type, decorators), typeInformation.Type, decorators));
                }
            }

            var exprOverride = resolutionContext.GetExpressionOverrideOrDefault(typeInformation.Type, typeInformation.DependencyName);

            if (exprOverride != null)
            {
                return(exprOverride);
            }

            var registration = resolutionContext
                               .CurrentContainerContext
                               .RegistrationRepository
                               .GetRegistrationOrDefault(typeInformation, resolutionContext);

            resolutionContext.IsTopRequest = false;
            return(registration != null
                ? this.BuildExpressionForRegistration(registration, resolutionContext, typeInformation)
                : this.BuildResolutionExpressionUsingResolvers(typeInformation, resolutionContext));
        }
Пример #7
0
 private IEnumerable <Expression> BuildAllResolverExpressionsUsingResolvers(TypeInformation typeInfo,
                                                                            ResolutionContext resolutionContext) =>
 this.resolverRepository.BuildAllResolutionExpressions(typeInfo, resolutionContext, this) ??
 this.lastChanceResolverRepository.BuildAllResolutionExpressions(typeInfo, resolutionContext, this);
Пример #8
0
 public bool CanResolveType(TypeInformation typeInfo, ResolutionContext resolutionContext) =>
 this.resolverRepository.CanResolve(typeInfo, resolutionContext) ||
 this.lastChanceResolverRepository.CanResolve(typeInfo, resolutionContext);
Пример #9
0
        public Expression BuildExpressionForRegistration(ServiceRegistration serviceRegistration,
                                                         ResolutionContext resolutionContext, TypeInformation typeInformation)
        {
            var requestedType = typeInformation.Type;

            if (serviceRegistration is OpenGenericRegistration openGenericRegistration)
            {
                serviceRegistration = openGenericRegistration.ProduceClosedRegistration(requestedType);
            }

            var decorators = resolutionContext
                             .CurrentContainerContext
                             .DecoratorRepository
                             .GetDecoratorsOrDefault(serviceRegistration.ImplementationType, typeInformation, resolutionContext);

            if (decorators == null)
            {
                return(this.BuildExpressionAndApplyLifetime(serviceRegistration, resolutionContext, requestedType));
            }

            var stack = decorators.AsStack();

            stack.PushBack(serviceRegistration);
            return(this.BuildExpressionForDecorator(stack.Front(),
                                                    resolutionContext.BeginDecoratingContext(requestedType, stack), requestedType, stack));
        }
Пример #10
0
        public Expression[] GetResolverExpressions(IContainerContext containerContext, TypeInformation typeInfo, ResolutionInfo resolutionInfo)
        {
            for (var i = 0; i < this.resolverRepository.Lenght; i++)
            {
                var item = this.resolverRepository.Get(i);
                if (item.SupportsMany && item.CanUseForResolution(containerContext, typeInfo, resolutionInfo))
                {
                    return(item.GetExpressions(containerContext, typeInfo, resolutionInfo));
                }
            }

            return(this.parentContainerResolver.CanUseForResolution(containerContext, typeInfo, resolutionInfo) ? this.parentContainerResolver.GetExpressions(containerContext, typeInfo, resolutionInfo) : null);
        }