/// <inheritdoc />
 public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, Type type)
 {
     if (!type.IsInterface)
     {
         yield return(new ServiceBinding(type, BindingMetadata.GeneratedInstance, graph.Settings.ConstructorResolver.ResolveConstructorExpression(type), type, graph.Settings.ConstructorResolver));
     }
 }
Example #2
0
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, Type type)
        {
            if (type.IsGenericType)
            {
                Type definition = type.GetGenericTypeDefinition();
                if (definition == typeof(IEnumerable <>) || definition == typeof(IReadOnlyCollection <>) || definition == typeof(IReadOnlyList <>))
                {
                    Type elementType = type.GenericTypeArguments[0];

                    Func <Scoped, object>[] instanceFactories = graph.TryResolveAll(elementType).Select(x => x.Factory).ToArray();

                    Type       instanceFactoryListType = typeof(InstanceFactoryList <>).MakeGenericType(type.GenericTypeArguments);
                    Expression expression = Expression.New(ConstructorResolvers.Default.StaticSelectConstructor(instanceFactoryListType), ExpressionGenerator.ScopeParameter, Expression.Constant(instanceFactories));

                    Type[] types =
                    {
                        typeof(IEnumerable <>).MakeGenericType(elementType),
                        typeof(IReadOnlyCollection <>).MakeGenericType(elementType),
                        typeof(IReadOnlyList <>).MakeGenericType(elementType)
                    };

                    foreach (Type newType in types)
                    {
                        yield return(new ServiceBinding(newType, BindingMetadata.GeneratedInstance, expression, instanceFactoryListType, graph.Settings.ConstructorResolver));
                    }
                }
            }
        }
Example #3
0
 public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, Type type)
 {
     if (!type.IsInterface && !type.IsGenericType)
     {
         yield return(new ServiceBinding(type, new BindingMetadata(), type.AutoResolveConstructorExpression()));
     }
 }
        public IEnumerable <Binding> Resolve(IResolverPipeline graph, 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         dependencyType = funcType.GenericTypeArguments[0];
                    Registration registration   = graph.GetDependency(dependencyType);

                    MethodInfo method = GenericCreateLambdaMethod.MakeGenericMethod(dependencyType);

                    foreach (Binding binding in registration.Bindings)
                    {
                        Expression baseExpression = graph.ResolveDependency(dependencyType, binding).Expression;
                        var        newBinding     = new Binding(new BindingMetadata(type), baseExpression);

                        var expression = (Expression)method.Invoke(null, new object[] { baseExpression });
                        var factory    = new InstanceFactory(type, expression, scoped => expression);
                        newBinding.Factories.Add(factory);
                        yield return(newBinding);
                    }
                }
            }
        }
Example #5
0
        public IEnumerable <Binding> Resolve(IResolverPipeline graph, Type type)
        {
            if (type.IsGenericType)
            {
                Type definition = type.GetGenericTypeDefinition();
                if (definition == typeof(IEnumerable <>) || definition == typeof(IReadOnlyCollection <>) || definition == typeof(IReadOnlyList <>))
                {
                    Type         elementType  = type.GenericTypeArguments[0];
                    Registration?registration = graph.TryGetDependency(elementType);
                    Binding[]    dependencies = registration?.Bindings.Array ?? new Binding[0];

                    Func <Scoped, object>[] instanceFactories = dependencies.Select(x => graph.ResolveDependency(elementType, x).Factory).ToArray();

                    Type       instanceFactoryListType = typeof(InstanceFactoryList <>).MakeGenericType(type.GenericTypeArguments);
                    Expression expression = Expression.New(instanceFactoryListType.AutoResolveConstructor(), ExpressionGenerator.ScopeParameter, Expression.Constant(instanceFactories));

                    Type[] types =
                    {
                        typeof(IEnumerable <>).MakeGenericType(elementType),
                        typeof(IReadOnlyCollection <>).MakeGenericType(elementType),
                        typeof(IReadOnlyList <>).MakeGenericType(elementType)
                    };

                    foreach (Type newType in types)
                    {
                        yield return(new Binding(new BindingMetadata(newType), expression));
                    }
                }
            }
        }
Example #6
0
 public void SetUp()
 {
     _subject = new ResolverPipeline();
     _subject.AddHandlers(
         new TestComponentHandler("Foo", _foo = new TestComponent()),
         new TestComponentHandler("Bar", _bar = new TestComponent())
         );
 }
Example #7
0
 public void SetUp()
 {
     _subject = new ResolverPipeline();
     _subject.AddHandlers(
         new LazyHandler(_subject),
         new TestComponentHandler("Foo", _testable = new TestComponent())
         );
 }
Example #8
0
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, Type type)
        {
            if (type == typeof(Container))
            {
                Expression expression = Expression.Call(null, _getContainer, ExpressionGenerator.ScopeParameter);
                yield return(new ServiceBinding(type, BindingMetadata.GeneratedInstance, expression, type, graph.Settings.ConstructorResolver, 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, graph.Settings.ConstructorResolver, Lifetimes.PerContainer, null, ServiceAutoDispose.Never));
            }
        }
        public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, Type type)
        {
            if (type.IsGenericType && !type.ContainsGenericParameters)
            {
                Type            genericTypeDefinition = type.GetGenericTypeDefinition();
                InstanceFactory openGenericFactory    = graph.Resolve(genericTypeDefinition);
                var             openGenericBinding    = (ServiceBinding)((ConstantExpression)openGenericFactory.Expression).Value;

                Type       openGenericType   = ((AbstractBindingExpression)openGenericBinding.Expression !).Type;
                Type       closedGenericType = openGenericType.MakeGenericType(type.GenericTypeArguments);
                Expression newExpression     = closedGenericType.AutoResolveConstructorExpression();

                yield return(new ServiceBinding(type, openGenericBinding.BindingMetadata, newExpression, openGenericBinding.Lifetime, openGenericBinding.Finalizer, openGenericBinding.NeedsDispose));
            }
        }
 public void SetUp()
 {
     _pipeline = new ResolverPipeline();
     _pipeline.AddHandlers(
         new TestComponentHandler("Foo", _expected = new TestComponent())
         );
     _goodCtor = typeof (MultiConstructor).GetConstructor(new[] {typeof (ITestComponent)});
     _badCtor = typeof(MultiConstructor).GetConstructor(new[] { typeof(ITestComponent), typeof(int) });
     _defCtor = typeof (MultiConstructor).GetConstructor(new Type[] {});
     _subject = new ConstructorResolver(_pipeline);
     _request = new ResolverRequest(
         typeof(MultiConstructor),
         null,
         new ResolverArgs(new {Key = "Foo"}));
 }
        public IEnumerable <Binding> Resolve(IResolverPipeline graph, Type type)
        {
            if (type.IsGenericType && !type.ContainsGenericParameters)
            {
                Type         genericTypeDefinition = type.GetGenericTypeDefinition();
                Registration?genericRegistration   = graph.TryGetDependency(genericTypeDefinition);
                if (genericRegistration != null)
                {
                    Binding    genericBinding    = genericRegistration.Default;
                    Type       openGenericType   = ((OpenGenericTypeExpression)genericBinding.Expression !).Type;
                    Type       closedGenericType = openGenericType.MakeGenericType(type.GenericTypeArguments);
                    Expression newExpression     = closedGenericType.AutoResolveConstructorExpression();

                    yield return(new Binding(new BindingMetadata(type), newExpression, genericBinding.Lifetime));
                }
            }
        }
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Func <>))
            {
                Type dependencyType = type.GenericTypeArguments[0];

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

                    yield return(new ServiceBinding(type, BindingMetadata.GeneratedInstance, baseExpression, type, graph.Settings.ConstructorResolver)
                    {
                        BaseExpression = new ExpressionContext(baseExpression)
                    });
                }
            }
        }
        public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Func <>))
            {
                Type dependencyType = type.GenericTypeArguments[0];

                foreach (InstanceFactory factory in graph.ResolveAll(dependencyType))
                {
                    LambdaExpression baseExpression = Expression.Lambda(factory.Expression);

                    yield return(new ServiceBinding(type, new BindingMetadata(), baseExpression)
                    {
                        BaseExpression = baseExpression
                    });
                }
            }
        }
Example #14
0
        public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, 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 graph.ResolveAll(funcType))
                {
                    NewExpression baseExpression = Expression.New(constructor, factory.Expression);
                    var           newBinding     = new ServiceBinding(type, new BindingMetadata(), baseExpression);
                    newBinding.Factories.Add(new InstanceFactory(type, baseExpression));
                    yield return(newBinding);
                }
            }
        }
        public IEnumerable <Binding> Resolve(IResolverPipeline graph, Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Func <>))
            {
                Type         dependencyType = type.GenericTypeArguments[0];
                Registration dependency     = graph.GetDependency(dependencyType);

                foreach (Binding binding in dependency.Bindings)
                {
                    InstanceFactory  factory        = graph.ResolveDependency(dependencyType, binding);
                    LambdaExpression baseExpression = Expression.Lambda(factory.Expression);

                    yield return(new Binding(new BindingMetadata(type), baseExpression)
                    {
                        BaseExpression = baseExpression
                    });
                }
            }
        }
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, 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 graph.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, graph.Settings.ConstructorResolver);
                    newBinding.Factories.Add(new InstanceFactory(type, context));
                    yield return(newBinding);
                }
            }
        }
        public IEnumerable <Binding> Resolve(IResolverPipeline graph, 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]);
                Registration factoryDependency = graph.GetDependency(funcType);

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

                foreach (Binding binding in factoryDependency.Bindings)
                {
                    NewExpression baseExpression = Expression.New(constructor, graph.ResolveDependency(lazyType, binding).Expression);
                    var           newBinding     = new Binding(new BindingMetadata(type), baseExpression);
                    newBinding.Factories.Add(new InstanceFactory(type, baseExpression));
                    yield return(newBinding);
                }
            }
        }
Example #18
0
 private static void CheckChildRegistrations(IResolverPipeline parentDependencyGraph, Dictionary <Type, Registration> registrations, object syncRoot)
 {
     lock (syncRoot)
     {
         foreach (Registration registration in registrations.Values)
         {
             Type type = registration.DependencyType;
             if (parentDependencyGraph.Dependencies.TryGetValue(type, out _))
             {
                 throw new RegistrationAlreadyExistsException($"Dependency {type} was already registered in the parent graph!");
             }
             else if (type.IsGenericType)
             {
                 if (parentDependencyGraph.Dependencies.TryGetValue(type.GetGenericTypeDefinition(), out _))
                 {
                     throw new RegistrationAlreadyExistsException($"Dependency {type} was already registered as a open generic in the parent graph!");
                 }
             }
         }
     }
 }
        /// <inheritdoc />
        public IEnumerable <ServiceBinding> Resolve(IResolverPipeline graph, 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       dependencyType = funcType.GenericTypeArguments[0];
                    MethodInfo method         = GenericCreateLambdaMethod.MakeGenericMethod(dependencyType);
                    foreach (InstanceFactory instanceFactory in graph.TryResolveAll(dependencyType))
                    {
                        var newBinding = new ServiceBinding(type, BindingMetadata.GeneratedInstance, instanceFactory.Context.Expression, instanceFactory.Context.Expression.Type, graph.Settings.ConstructorResolver);

                        var expression = (Expression)method.Invoke(null, new object[] { instanceFactory.Context });
                        var factory    = new InstanceFactory(type, new ExpressionContext(expression), scoped => expression);
                        newBinding.Factories.Add(factory);
                        yield return(newBinding);
                    }
                }
            }
        }
Example #20
0
        public ConstructorInfo DynamicSelectConstructor(Type type, IResolverPipeline resolverPipeline)
        {
            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 => resolverPipeline.TryResolve(x.ParameterType) != null))
                    {
                        return(constructorInfo);
                    }
                }
            }
            return(constructors.FirstOrDefault());
        }
Example #21
0
 public ConstructorResolver(IResolverPipeline pipeline)
 {
     _pipeline = pipeline;
 }
 public ConstructorInfo DynamicSelectConstructor(Type type, IResolverPipeline resolverPipeline)
 {
     return(_constructorResolver.DynamicSelectConstructor(type, resolverPipeline));
 }
 public ConstructorInfo DynamicSelectConstructor(Type type, IResolverPipeline resolverPipeline)
 {
     throw new NotImplementedException();
 }
Example #24
0
        public DependencyGraph(RegistrationStore registrations, Scoped scope, SingularitySettings settings, DependencyGraph?parentDependencyGraph = null)
        {
            var resolvers = new IDependencyResolver[] { new EnumerableDependencyResolver(), new ExpressionDependencyResolver(), new LazyDependencyResolver(), new FactoryDependencyResolver(), new ConcreteDependencyResolver(), new OpenGenericResolver() };

            _resolverPipeline = new ResolverPipeline(registrations, resolvers, scope, settings, parentDependencyGraph?._resolverPipeline);
        }
Example #25
0
 public LazyHandler(IResolverPipeline pipeline)
 {
     _pipeline = pipeline;
 }
Example #26
0
        public ComponentHandler(IResolverPipeline pipeline)
        {
            if (pipeline == null) throw new ArgumentNullException("pipeline");

            _pipeline = pipeline;
        }
Example #27
0
 public FactoryHandler(IResolverPipeline pipeline)
 {
     _pipeline = pipeline;
 }