Exemplo n.º 1
0
        private Expression PrepareDefaultExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext)
        {
            if (serviceRegistration.RegistrationContext.DefinedScopeName == null)
            {
                return(this.expressionFactory.ConstructExpression(serviceRegistration, resolutionContext));
            }

            var variable = Constants.ResolutionScopeType.AsVariable();

            var newScope = resolutionContext.CurrentScopeParameter
                           .CallMethod(Constants.BeginScopeMethod,
                                       serviceRegistration.RegistrationContext.DefinedScopeName.AsConstant(),
                                       true.AsConstant());

            var newScopeContext = resolutionContext.BeginNewScopeContext(new KeyValue <object, ParameterExpression>(serviceRegistration.RegistrationContext.DefinedScopeName, variable));

            resolutionContext.AddDefinedVariable(variable);
            resolutionContext.AddInstruction(variable.AssignTo(newScope.ConvertTo(Constants.ResolutionScopeType)));

            var expression = this.expressionFactory.ConstructExpression(serviceRegistration, newScopeContext);

            foreach (var definedVariable in newScopeContext.DefinedVariables.Walk())
            {
                resolutionContext.AddDefinedVariable(definedVariable);
            }

            foreach (var instruction in newScopeContext.SingleInstructions)
            {
                resolutionContext.AddInstruction(instruction);
            }

            return(expression);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Stores the given expression in a local variable and saves it into the resolution context for further reuse.
        /// </summary>
        /// <param name="expression">The expression to store.</param>
        /// <param name="resolutionContext">The resolution context.</param>
        /// <param name="resolveType">The resolve type.</param>
        /// <returns>The local variable.</returns>
        public Expression StoreExpressionIntoLocalVariable(Expression expression, ResolutionContext resolutionContext, Type resolveType)
        {
            var variable = resolveType.AsVariable();

            resolutionContext.AddDefinedVariable(this.ScopeId, variable);
            resolutionContext.AddInstruction(variable.AssignTo(expression));
            return(variable);
        }
Exemplo n.º 3
0
        private Expression PrepareExpression(IContainerContext containerContext, IServiceRegistration serviceRegistration, ResolutionContext resolutionContext, Type resolveType)
        {
            if (serviceRegistration.RegistrationContext.DefinedScopeName == null)
            {
                return(this.expressionBuilder.CreateExpression(containerContext, serviceRegistration, resolutionContext, resolveType));
            }

            var variable = Constants.ResolutionScopeType.AsVariable();

            var newScope = resolutionContext.CurrentScopeParameter
                           .ConvertTo(Constants.ResolverType)
                           .CallMethod(Constants.BeginScopeMethod,
                                       serviceRegistration.RegistrationContext.DefinedScopeName.AsConstant(),
                                       true.AsConstant());


            resolutionContext.AddDefinedVariable(variable);
            resolutionContext.AddInstruction(variable.AssignTo(newScope.ConvertTo(Constants.ResolutionScopeType)));

            var newContext = resolutionContext.Clone(scopeParameter:
                                                     new KeyValue <object, ParameterExpression>(serviceRegistration.RegistrationContext.DefinedScopeName, variable));

            var expression = this.expressionBuilder.CreateExpression(containerContext, serviceRegistration, newContext, resolveType);

            foreach (var definedVariable in newContext.DefinedVariables.Repository)
            {
                resolutionContext.AddDefinedVariable(definedVariable.Key, definedVariable.Value);
            }

            foreach (var instruction in newContext.SingleInstructions)
            {
                resolutionContext.AddInstruction(instruction);
            }

            return(expression);
        }
Exemplo n.º 4
0
        internal Expression ApplyLifetime(ExpressionBuilder expressionBuilder, ServiceRegistration serviceRegistration,
                                          ResolutionContext resolutionContext, Type requestedType)
        {
            if (resolutionContext.CurrentContainerContext.ContainerConfiguration.LifetimeValidationEnabled &&
                this.LifeSpan > 0)
            {
                if (resolutionContext.CurrentLifeSpan > this.LifeSpan)
                {
                    throw new LifetimeValidationFailedException(serviceRegistration.ImplementationType,
                                                                $"The life-span of {serviceRegistration.ImplementationType} ({this.Name}|{this.LifeSpan}) " +
                                                                $"is shorter than its direct or indirect parent's {resolutionContext.NameOfServiceLifeSpanValidatingAgainst}." + Environment.NewLine +
                                                                "This could lead to incidental lifetime promotions with longer life-span, it's recommended to double check your lifetime configurations.");
                }

                resolutionContext = resolutionContext.BeginLifetimeValidationContext(this.LifeSpan,
                                                                                     $"{serviceRegistration.ImplementationType} ({this.Name}|{this.LifeSpan})");
            }

            if (!this.StoreResultInLocalVariable)
            {
                return(this.BuildLifetimeAppliedExpression(expressionBuilder,
                                                           serviceRegistration, resolutionContext, requestedType));
            }

            var variable = resolutionContext.GetKnownVariableOrDefault(serviceRegistration.RegistrationId);

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

            var resultExpression = this.BuildLifetimeAppliedExpression(expressionBuilder,
                                                                       serviceRegistration, resolutionContext, requestedType);

            if (resultExpression == null)
            {
                return(null);
            }

            variable = requestedType.AsVariable();
            resolutionContext.AddDefinedVariable(serviceRegistration.RegistrationId, variable);
            resolutionContext.AddInstruction(variable.AssignTo(resultExpression));
            return(variable);
        }
        public Expression[] GetExpressions(IContainerContext containerContext, TypeInformation typeInfo, ResolutionContext resolutionContext)
        {
            var resolution = resolutionContext.ChildContext == null
                ? resolutionContext.Clone(containerContext)
                : resolutionContext.Clone(resolutionContext.ChildContext);

            var result = containerContext.ResolutionStrategy
                         .BuildResolutionExpressions(containerContext.Container.ParentContainer.ContainerContext, resolution, typeInfo);

            foreach (var definedVariable in resolution.DefinedVariables.Repository)
            {
                resolutionContext.AddDefinedVariable(definedVariable.Key, definedVariable.Value);
            }

            foreach (var instruction in resolution.SingleInstructions)
            {
                resolutionContext.AddInstruction(instruction);
            }

            return(result);
        }
Exemplo n.º 6
0
        private Expression PrepareExpression(IContainerContext containerContext, IServiceRegistration serviceRegistration, ResolutionContext resolutionContext, Type resolveType)
        {
            if (serviceRegistration.RegistrationContext.DefinedScopeName != null)
            {
                var variable = Constants.ResolutionScopeType.AsVariable();

                var newScope = resolutionContext.CurrentScopeParameter
                               .ConvertTo(Constants.ResolverType)
                               .CallMethod(Constants.BeginScopeMethod,
                                           serviceRegistration.RegistrationContext.DefinedScopeName.AsConstant(),
                                           true.AsConstant());


                resolutionContext.AddDefinedVariable(variable);
                resolutionContext.AddInstruction(variable.AssignTo(newScope.ConvertTo(Constants.ResolutionScopeType)));

                return(this.expressionBuilder.CreateExpression(containerContext,
                                                               serviceRegistration, resolutionContext.CreateNew(scopeParameter:
                                                                                                                new KeyValue <object, ParameterExpression>(serviceRegistration.RegistrationContext.DefinedScopeName, variable)),
                                                               resolveType));
            }

            return(this.expressionBuilder.CreateExpression(containerContext, serviceRegistration, resolutionContext, resolveType));
        }