Ejemplo n.º 1
0
        private InstanceFactory GenerateInstanceFactory(Type type, ServiceBinding serviceBinding, CircularDependencyDetector circularDependencyDetector)
        {
            lock (serviceBinding)
            {
                if (!serviceBinding.TryGetInstanceFactory(type, out InstanceFactory factory))
                {
                    if (serviceBinding.Expression is AbstractBindingExpression)
                    {
                        factory = new InstanceFactory(type, (ExpressionContext)serviceBinding.BaseExpression !, scoped => throw new AbstractTypeResolveException($"Cannot create a instance for type {type} since its registered as a abstract binding and not meant to be used directly."));
                        serviceBinding.Factories.Add(factory);
                        return(factory);
                    }

                    Expression[]          decorators = FindDecorators(type);
                    ParameterExpression[] parameters = decorators.GetParameterExpressions().Where(x => x.Type != ExpressionGenerator.ScopeParameter.Type && x.Type != type).ToArray();
                    var childFactories = new InstanceFactory[parameters.Length];
                    for (var i = 0; i < parameters.Length; i++)
                    {
                        ParameterExpression parameter = parameters[i];
                        ServiceBinding      child     = GetDependency(parameter.Type).Default;
                        childFactories[i] = ResolveDependency(parameter.Type, child, circularDependencyDetector);
                    }

                    ReadOnlyExpressionContext context = _expressionGenerator.ApplyDecorators(type, serviceBinding, childFactories, decorators, _containerScope);
                    factory = new InstanceFactory(type, context);
                    serviceBinding.Factories.Add(factory);
                }
                return(factory);
            }
        }
Ejemplo n.º 2
0
 private InstanceFactory GenerateInstanceFactory(Type type, ServiceBinding serviceBinding, CircularDependencyDetector circularDependencyDetector)
 {
     lock (serviceBinding)
     {
         if (!serviceBinding.TryGetInstanceFactory(type, out InstanceFactory factory))
         {
             Expression[]              decorators = FindDecorators(type);
             ParameterExpression[]     parameters = decorators.GetParameterExpressions().Where(x => x.Type != ExpressionGenerator.ScopeParameter.Type && x.Type != type).ToArray();
             InstanceFactory[]         factories  = ResolveParameters(parameters, circularDependencyDetector);
             ReadOnlyExpressionContext context    = _expressionGenerator.ApplyDecorators(type, serviceBinding, factories, decorators, _containerScope);
             factory = new InstanceFactory(type, context);
             serviceBinding.Factories.Add(factory);
         }
         return(factory);
     }
 }