public void Verify(InjectionConsumerInfo consumer)
 {
     if (!this.convention.CanResolve(consumer.Target))
     {
         this.decorated.Verify(consumer);
     }
 }
        public void Verify_TValueTypeParameter_ThrowsExpectedException()
        {
            // Arrange
            string expectedString = string.Format(@"
                The constructor of type {0}.{1} contains parameter 'intArgument' of type Int32 which can not 
                be used for constructor injection because it is a value type.",
                this.GetType().Name,
                typeof(TypeWithSinglePublicConstructorWithValueTypeParameter).Name)
                .TrimInside();

            var behavior = new Container().Options.DependencyInjectionBehavior;

            var constructor =
                typeof(TypeWithSinglePublicConstructorWithValueTypeParameter).GetConstructors().Single();

            var consumer = new InjectionConsumerInfo(
                constructor.DeclaringType,
                constructor.DeclaringType,
                constructor.GetParameters().Single());

            try
            {
                // Act
                behavior.Verify(consumer);

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                AssertThat.StringContains(expectedString, ex.Message);
            }
        }
        public void Verify(InjectionConsumerInfo consumer)
        {
            var parameter = consumer.Target.Parameter;

            if (parameter != null && !IsOptional(parameter))
            {
                this.original.Verify(consumer);
            }
        }
            public Expression BuildExpression(InjectionConsumerInfo consumer)
            {
                if (!this.convention.CanResolve(consumer.Target))
                {
                    return this.decorated.BuildExpression(consumer);
                }

                return this.convention.BuildExpression(consumer);
            }
        public Expression BuildExpression(InjectionConsumerInfo consumer)
        {
            Requires.IsNotNull(consumer, nameof(consumer));

            InstanceProducer producer = this.GetInstanceProducerFor(consumer);
            
            // When the instance producer is invalid, this call will fail with an expressive exception.
            return producer.BuildExpression();
        }
        public void Verify(InjectionConsumerInfo consumer)
        {
            Requires.IsNotNull(consumer, nameof(consumer));

            var target = consumer.Target;

            if (target.TargetType.IsValueType || target.TargetType == typeof(string))
            {
                throw new ActivationException(StringResources.TypeMustNotContainInvalidInjectionTarget(target));
            }
        }
        public Expression BuildExpression(InjectionConsumerInfo consumer)
        {
            var parameter = consumer.Target.Parameter;

            if (parameter != null && IsOptional(parameter) && !this.CanBeResolved(parameter))
            {
                return Expression.Constant(parameter.DefaultValue, parameter.ParameterType);
            }

            return this.original.BuildExpression(consumer);
        }
        private InstanceProducer GetInstanceProducerFor(InjectionConsumerInfo consumer)
        {
            InjectionTargetInfo target = consumer.Target;

            InstanceProducer producer = this.container.GetRegistrationEvenIfInvalid(target.TargetType, consumer);
            
            if (producer == null)
            {
                // By redirecting to Verify() we let the verify throw an expressive exception. If it doesn't
                // we throw the exception ourselves.
                this.container.Options.DependencyInjectionBehavior.Verify(consumer);

                this.container.ThrowParameterTypeMustBeRegistered(target);
            }

            return producer;
        }
            public Expression BuildExpression(InjectionConsumerInfo consumer)
            {
                var expression = this.defaultBehavior.BuildExpression(consumer);

                List<PredicatePair> predicatePairs;

                if (this.MustApplyContextualDecorator(consumer.Target.TargetType, out predicatePairs))
                {
                    var visitor = new ContextualDecoratorExpressionVisitor(consumer.Target, predicatePairs);

                    expression = visitor.Visit(expression);

                    if (!visitor.AllContextualDecoratorsApplied)
                    {
                        throw new InvalidOperationException("Couldn't apply the contextual decorator " + 
                            visitor.UnappliedDecorators.Last().FullName + ". Make sure that all registered " +
                            "decorators that wrap this decorator are transient and don't depend on " +
                            "Func<" + consumer.Target.TargetType.FullName + ">.");
                    }
                }

                return expression;
            }
 public InstanceProducer TryGetProducer(InjectionConsumerInfo consumer, bool handled) =>
     this.producer.Predicate(new PredicateContext(this.producer, consumer, handled))
         ? this.producer
         : null;
        private IEnumerable<InstanceProducer> GetInstanceProducers(InjectionConsumerInfo consumer)
        {
            bool handled = false;

            foreach (var provider in this.providers)
            {
                InstanceProducer producer = provider.TryGetProducer(consumer, handled);

                if (producer != null)
                {
                    yield return producer;
                    handled = true;
                }
            }
        }
예제 #12
0
 public InstanceProducer GetInstanceProducer(InjectionConsumerInfo dependency, bool @throw)
 {
     throw new NotImplementedException();
 }
            private Type GetImplementationTypeThroughFactory(Type serviceType, InjectionConsumerInfo consumer)
            {
                Type implementationType =
                    this.ImplementationTypeFactory(new TypeFactoryContext(serviceType, consumer));

                if (implementationType == null)
                {
                    throw new InvalidOperationException(StringResources.FactoryReturnedNull(this.ServiceType));
                }

                if (implementationType.ContainsGenericParameters)
                {
                    Requires.TypeFactoryReturnedTypeThatDoesNotContainUnresolvableTypeArguments(
                        serviceType, implementationType);

                    // implementationType == null when type constraints don't match.
                    implementationType =
                        GenericTypeBuilder.MakeClosedImplementation(serviceType, implementationType);
                }
                else
                {
                    Requires.FactoryReturnsATypeThatIsAssignableFromServiceType(serviceType, implementationType);
                }

                return implementationType;
            }
 private bool MatchesPredicate(InjectionConsumerInfo consumer, bool handled) =>
     this.producer.Predicate(new PredicateContext(this.producer, consumer, handled));
        private IEnumerable<Tuple<Type, Type, InstanceProducer>> GetInstanceProducers(
            Type closedGenericServiceType, InjectionConsumerInfo consumer)
        {
            bool handled = false;

            foreach (var provider in this.providers)
            {
                var producer =
                    provider.TryGetProducer(closedGenericServiceType, consumer, handled: handled);

                if (producer != null)
                {
                    yield return Tuple.Create(
                        item1: provider.ServiceType,
                        item2: provider.ImplementationType ?? producer.ImplementationType,
                        item3: producer);

                    handled = true;
                }
            }
        }
        public InstanceProducer TryGetInstanceProducer(Type serviceType, InjectionConsumerInfo context)
        {
            var instanceProducers = this.GetInstanceProducers(context).ToArray();

            if (instanceProducers.Length <= 1)
            {
                return instanceProducers.FirstOrDefault();
            }

            throw this.ThrowMultipleApplicableRegistrationsFound(instanceProducers);
        }
            private IEnumerable<InstanceProducer> GetInstanceProducers(InjectionConsumerInfo consumer)
            {
                bool handled = false;

                foreach (var producer in this.producers)
                {
                    var context = new PredicateContext(producer, consumer, handled);
                    if (!producer.IsConditional || producer.Predicate(context))
                    {
                        yield return producer;
                        handled = true;
                    }
                }
            }
 private InstanceProducer GetProducer(InjectionConsumerInfo dependency) =>
 this.injectionBehavior.GetInstanceProducer(dependency, throwOnFailure: false);
 public void Verify(InjectionConsumerInfo consumer)
 {
 }
 public Expression BuildExpression(InjectionConsumerInfo consumer) => this.ExpressionToReturn;
        private Expression GetPropertyExpression(PropertyInfo property)
        {
            var consumer = new InjectionConsumerInfo(this.serviceType, this.implementationType, property);

            return this.container.Options.DependencyInjectionBehavior.BuildExpression(consumer);
        }
예제 #22
0
            public InstanceProducer TryGetInstanceProducer(Type serviceType, InjectionConsumerInfo context)
            {
                Requires.IsTrue(serviceType == this.ServiceType, "serviceType");

                var instanceProducers = this.GetInstanceProducers(context).ToArray();

                if (instanceProducers.Length <= 1)
                {
                    return instanceProducers.FirstOrDefault();
                }

                var producersInfo =
                    from producer in instanceProducers
                    select Tuple.Create(this.ServiceType, producer.Registration.ImplementationType, producer);

                throw new ActivationException(
                    StringResources.MultipleApplicationRegistrationsFound(
                        this.ServiceType, producersInfo.ToArray()));
            }
        public Expression BuildExpression(InjectionConsumerInfo consumer)
        {
            string connectionString = GetConnectionString(consumer.Target);

            return Expression.Constant(connectionString, typeof(string));
        }
            public InstanceProducer TryGetProducer(InjectionConsumerInfo consumer, bool handled)
            {
                Func<Type> implementationTypeProvider = 
                    () => this.GetImplementationTypeThroughFactory(consumer);

                var context = 
                    new PredicateContext(this.serviceType, implementationTypeProvider, consumer, handled);

                // NOTE: The producer should only get built after it matches the delegate, to prevent
                // unneeded producers from being created, because this might cause diagnostic warnings, 
                // such as torn lifestyle warnings.
                return this.predicate(context) ? this.GetProducer(context) : null;
            }
            private Type GetImplementationTypeThroughFactory(InjectionConsumerInfo consumer)
            {
                var context = new TypeFactoryContext(this.serviceType, consumer);

                Type implementationType = this.implementationTypeFactory(context);

                if (implementationType == null)
                {
                    throw new InvalidOperationException(StringResources.FactoryReturnedNull(this.serviceType));
                }

                if (implementationType.ContainsGenericParameters())
                {
                    throw new ActivationException(
                        StringResources.TheTypeReturnedFromTheFactoryShouldNotBeOpenGeneric(
                            this.serviceType, implementationType));
                }

                Requires.FactoryReturnsATypeThatIsAssignableFromServiceType(this.serviceType, implementationType);

                return implementationType;
            }
 public Expression BuildExpression(InjectionConsumerInfo dependency) =>
 this.GetProducer(dependency)?.BuildExpression() ?? GetDefault(dependency.Target.Parameter);
        public InstanceProducer TryGetInstanceProducer(Type closedGenericServiceType,
            InjectionConsumerInfo context)
        {
            var producers = this.GetInstanceProducers(closedGenericServiceType, context).ToArray();

            if (producers.Length <= 1)
            {
                return producers.Select(p => p.Item3).FirstOrDefault();
            }

            throw new ActivationException(
                StringResources.MultipleApplicableRegistrationsFound(closedGenericServiceType, producers));
        }
        public Expression BuildExpression(InjectionConsumerInfo consumer)
        {
            object valueToInject = this.GetAppSettingValue(consumer.Target);

            return(Expression.Constant(valueToInject, consumer.Target.TargetType));
        }
 public InstanceProducer TryGetProducer(Type serviceType, InjectionConsumerInfo consumer,
     bool handled) =>
     this.MatchesServiceType(serviceType) && this.MatchesPredicate(consumer, handled)
         ? this.producer
         : null;
                public InstanceProducer TryGetProducer(Type serviceType, InjectionConsumerInfo consumer,
                    bool handled, bool unconditionally)
                {
                    InstanceProducer producer = this.GetOrBuildProducerFromCache(serviceType);

                    if (producer != null)
                    {
                        var context = new PredicateContext(producer, consumer, handled);

                        return unconditionally || this.MatchesPredicate(context)
                            ? producer
                            : null;
                    }

                    return null;
                }
            public InstanceProducer TryGetProducer(Type serviceType, InjectionConsumerInfo consumer,
                bool handled)
            {
                Type closedImplementation = this.ImplementationType != null
                    ? GenericTypeBuilder.MakeClosedImplementation(serviceType, this.ImplementationType)
                    : null;

                Func<Type> implementationTypeProvider = () => this.ImplementationType != null
                    ? closedImplementation
                    : this.GetImplementationTypeThroughFactory(serviceType, consumer);

                var context = new PredicateContext(serviceType, implementationTypeProvider, consumer, handled);

                // NOTE: The producer should only get built after it matches the delegate, to prevent
                // unneeded producers from being created, because this might cause diagnostic warnings, 
                // such as torn lifestyle warnings.
                var shouldBuildProducer =
                    (this.ImplementationType == null || closedImplementation != null)
                    && this.MatchesPredicate(context)
                    && context.ImplementationType != null;

                return shouldBuildProducer
                    ? this.GetProducer(serviceType, context.ImplementationType)
                    : null;
            }
 public void Verify(InjectionConsumerInfo consumer)
 {
     this.defaultBehavior.Verify(consumer);
 }
                public InstanceProducer TryGetProducer(Type serviceType, InjectionConsumerInfo consumer,
                    bool handled, bool unconditionally)
                {
                    bool match = this.MatchesServiceType(serviceType);

                    return match && (unconditionally || this.MatchesPredicate(consumer, handled))
                        ? this.producer
                        : null;
                }
예제 #34
0
 public bool VerifyDependency(InjectionConsumerInfo dependency, out string errorMessage)
 {
     throw new NotImplementedException();
 }
                private bool MatchesPredicate(InjectionConsumerInfo consumer, bool handled)
                {
                    if (this.producer.IsConditional)
                    {
                        var context = new PredicateContext(this.producer, consumer, handled);

                        return this.producer.Predicate(context);
                    }

                    return true;
                }
        public Expression BuildExpression(InjectionConsumerInfo consumer)
        {
            string connectionString = this.GetConnectionString(consumer.Target);

            return(Expression.Constant(connectionString, typeof(string)));
        }