Exemple #1
0
 /// <inheritdoc />
 public IEnumerable <ServiceBinding> Resolve(IInstanceFactoryResolver resolver, Type type)
 {
     if (!type.IsInterface && !type.IsAbstract && !type.IsPrimitive && type != typeof(string))
     {
         if (type.GetConstructorCandidates().Any())
         {
             yield return(new ServiceBinding(type, BindingMetadata.GeneratedInstance, resolver.Settings.ConstructorResolver.TryResolveConstructorExpression(type), type, resolver.Settings.ConstructorResolver, Lifetimes.Transient));
         }
     }
 }
Exemple #2
0
        private IEnumerable <ServiceBinding> Resolve <TElement>(IInstanceFactoryResolver resolver, Type type)
        {
            foreach (InstanceFactory instanceFactory in resolver.TryResolveAll(typeof(TElement)))
            {
                var newBinding = new ServiceBinding(type, BindingMetadata.GeneratedInstance, instanceFactory.Context.Expression, instanceFactory.Context.Expression.Type, ConstructorResolvers.Default, Lifetimes.Transient);

                var expression = Expression.Lambda <Func <TElement> >(ExpressionCompiler.OptimizeExpression(instanceFactory.Context));
                Func <Scoped, Expression <Func <TElement> > > del = scoped => expression; // we need to put this in a variable of this type or cast it else the static return type of the delegate will turn into a object..
                var factory = new InstanceFactory(type, new ExpressionContext(expression), del);
                newBinding.Factories.Add(factory);
                yield return(newBinding);
            }
        }
Exemple #3
0
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IInstanceFactoryResolver resolver, Type type)
        {
            if (type == typeof(Container))
            {
                Expression expression = Expression.Call(null, _getContainer, ExpressionGenerator.ScopeParameter);
                yield return(new ServiceBinding(type, BindingMetadata.GeneratedInstance, expression, type, ConstructorResolvers.Default, Lifetimes.PerContainer, null, ServiceAutoDispose.Never));
            }

            if (type == typeof(Scoped) || type == typeof(IServiceProvider))
            {
                Expression expression = Expression.Call(null, _getScope, ExpressionGenerator.ScopeParameter);
                yield return(new ServiceBinding(type, BindingMetadata.GeneratedInstance, expression, type, ConstructorResolvers.Default, Lifetimes.PerContainer, null, ServiceAutoDispose.Never));
            }
        }
Exemple #4
0
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IInstanceFactoryResolver resolver, Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Func <>))
            {
                Type dependencyType = type.GenericTypeArguments[0];

                foreach (InstanceFactory factory in resolver.TryResolveAll(dependencyType))
                {
                    LambdaExpression baseExpression = Expression.Lambda(factory.Context.Expression);

                    yield return(new ServiceBinding(type, BindingMetadata.GeneratedInstance, baseExpression, type, ConstructorResolvers.Default, Lifetimes.Transient)
                    {
                        BaseExpression = new ExpressionContext(baseExpression)
                    });
                }
            }
        }
Exemple #5
0
        private IEnumerable <ServiceBinding> Resolve <TElement>(IInstanceFactoryResolver resolver, Type type)
        {
            Func <Scoped, TElement>[] instanceFactories = resolver.TryResolveAll(typeof(TElement)).Select(x => (Func <Scoped, TElement>)(Delegate) x.Factory).ToArray();

            yield return(new ServiceBinding(new[]
            {
                typeof(Func <Scoped, TElement>[]),
            }, BindingMetadata.GeneratedInstance, Expression.Constant(instanceFactories), typeof(Func <Scoped, TElement>[]), ConstructorResolvers.Default, Lifetimes.PerContainer));

            Expression expression = ConstructorResolvers.Default.ResolveConstructorExpression(typeof(InstanceFactoryList <TElement>)) !;

            yield return(new ServiceBinding(new[]
            {
                typeof(IEnumerable <TElement>),
                typeof(IReadOnlyCollection <TElement>),
                typeof(IReadOnlyList <TElement>),
            }, BindingMetadata.GeneratedInstance, expression, expression.GetReturnType(), ConstructorResolvers.Default, Lifetimes.Transient));

            //lists
            Expression <Func <Scoped, List <TElement> > > listExpression = scope => CreateList(scope, instanceFactories);

            yield return(new ServiceBinding(new[]
            {
                typeof(List <TElement>),
                typeof(IList <TElement>),
                typeof(ICollection <TElement>),
            }, BindingMetadata.GeneratedInstance, listExpression, listExpression.GetReturnType(), ConstructorResolvers.Default, Lifetimes.Transient));

            //sets
            Expression <Func <Scoped, HashSet <TElement> > > setExpression = scope => CreateSet(scope, instanceFactories);

            yield return(new ServiceBinding(new[]
            {
                typeof(HashSet <TElement>),
                typeof(ISet <TElement>),
            }, BindingMetadata.GeneratedInstance, setExpression, setExpression.GetReturnType(), ConstructorResolvers.Default, Lifetimes.Transient));

            //arrays
            Expression <Func <Scoped, TElement[]> > arrayExpression = scope => CreateArray(scope, instanceFactories);

            yield return(new ServiceBinding(new[]
            {
                typeof(TElement[]),
            }, BindingMetadata.GeneratedInstance, arrayExpression, arrayExpression.GetReturnType(), ConstructorResolvers.Default, Lifetimes.Transient));
        }
Exemple #6
0
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IInstanceFactoryResolver resolver, Type type)
        {
            if (type.IsGenericType && !type.ContainsGenericParameters)
            {
                Type           genericTypeDefinition = type.GetGenericTypeDefinition();
                ServiceBinding?openGenericBinding    = resolver.TryGetBinding(genericTypeDefinition);
                if (openGenericBinding != null)
                {
                    Type       openGenericType   = openGenericBinding.ConcreteType;
                    Type       closedGenericType = openGenericType.MakeGenericType(type.GenericTypeArguments);
                    Expression?newExpression     = openGenericBinding.ConstructorResolver.ResolveConstructorExpression(closedGenericType);

                    yield return(new ServiceBinding(type, openGenericBinding.BindingMetadata, newExpression, closedGenericType,
                                                    openGenericBinding.ConstructorResolver, openGenericBinding.Lifetime, openGenericBinding.Finalizer,
                                                    openGenericBinding.NeedsDispose));
                }
            }
        }
Exemple #7
0
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IInstanceFactoryResolver resolver, Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Expression <>) && type.GenericTypeArguments.Length == 1)
            {
                Type funcType = type.GenericTypeArguments[0];
                if (funcType.GetGenericTypeDefinition() == typeof(Func <>) && funcType.GenericTypeArguments.Length == 1)
                {
                    Type       serviceType   = funcType.GenericTypeArguments[0];
                    MethodInfo resolveMethod = GenericResolveMethod.MakeGenericMethod(serviceType);

                    var bindings = (IEnumerable <ServiceBinding>)resolveMethod.Invoke(this, new object[] { resolver, type });
                    foreach (var binding in bindings)
                    {
                        yield return(binding);
                    }
                }
            }
        }
Exemple #8
0
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IInstanceFactoryResolver resolver, Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Lazy <>))
            {
                Type funcType = typeof(Func <>).MakeGenericType(type.GenericTypeArguments[0]);
                Type lazyType = typeof(Lazy <>).MakeGenericType(type.GenericTypeArguments[0]);

                ConstructorInfo constructor = lazyType.GetConstructor(new[] { funcType });

                foreach (InstanceFactory factory in resolver.TryResolveAll(funcType))
                {
                    var context = (ExpressionContext)factory.Context;
                    context.Expression = Expression.New(constructor, factory.Context.Expression);
                    var newBinding = new ServiceBinding(type, BindingMetadata.GeneratedInstance, context.Expression, type, ConstructorResolvers.Default, Lifetimes.Transient);
                    newBinding.Factories.Add(new InstanceFactory(type, context));
                    yield return(newBinding);
                }
            }
        }
Exemple #9
0
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IInstanceFactoryResolver resolver, Type type)
        {
            Type?elementType = null;

            if (type.IsArray)
            {
                elementType = type.GetElementType();
            }
            else if (type.IsGenericType)
            {
                Type openGenericCollectionType = type.GetGenericTypeDefinition();

                Type[] collectionTypes =
                {
                    typeof(IEnumerable <>),
                    typeof(IReadOnlyCollection <>),
                    typeof(IReadOnlyList <>),

                    typeof(List <>),
                    typeof(IList <>),
                    typeof(ICollection <>),

                    typeof(HashSet <>),
                    typeof(ISet <>),
                };
                if (collectionTypes.Contains(openGenericCollectionType))
                {
                    elementType = type.GenericTypeArguments[0];
                }
            }

            if (elementType != null)
            {
                MethodInfo resolveMethod = GenericResolveMethod.MakeGenericMethod(elementType);

                var bindings = (IEnumerable <ServiceBinding>)resolveMethod.Invoke(this, new object[] { resolver, type });
                foreach (var binding in bindings)
                {
                    yield return(binding);
                }
            }
        }
Exemple #10
0
        public ConstructorInfo DynamicSelectConstructor(Type type, IInstanceFactoryResolver instanceFactoryResolver)
        {
            ConstructorInfo[] constructors = type.GetConstructorCandidates().ToArray();
            if (constructors.Length == 0 && !type.IsValueType)
            {
                throw new NoConstructorException($"Type {type} did not contain any public constructor.");
            }

            if (constructors.Length > 1)
            {
                var ordering = constructors.OrderByDescending(x => x.GetParameters().Length);
                foreach (var constructorInfo in ordering)
                {
                    if (constructorInfo.GetParameters().All(x => instanceFactoryResolver.TryResolve(x.ParameterType) != null))
                    {
                        return(constructorInfo);
                    }
                }
            }
            return(constructors.FirstOrDefault());
        }
Exemple #11
0
 public ConstructorInfo DynamicSelectConstructor(Type type, IInstanceFactoryResolver instanceFactoryResolver)
 {
     throw new NotImplementedException();
 }
 /// <inheritdoc />
 public ConstructorInfo DynamicSelectConstructor(Type type, IInstanceFactoryResolver instanceFactoryResolver)
 {
     return(_constructorResolver.DynamicSelectConstructor(type, instanceFactoryResolver));
 }