Esempio n. 1
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);
            }
        }
Esempio n. 2
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)
                    });
                }
            }
        }
Esempio n. 3
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));
        }
Esempio n. 4
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);
                }
            }
        }