public void ExpressionRegistrationSetLifetimeThrowsExceptionIfInvalidLifetimeIsSupplied()
        {
            var lifetime = new TransientLifetime();
            var reg = new ExpressionRegistration<IFoo>(x => new Foo1(), lifetime, CompileMode.Delegate);

            reg.SetLifetime((ILifetime)null);
        }
		public void ExpressionRegistrationCanBeChanged()
		{
			var container = new IocContainer();
			var lifetime = new TransientLifetime();
			var reg = new ExpressionRegistration<IFoo>(container, x => new Foo1(), lifetime, CompileMode.Delegate);

			Assert.AreSame(reg.Lifetime, lifetime);
			Assert.AreEqual(reg.CompileMode, CompileMode.Delegate);

			var get1 = reg.GetInstance();

			var newLifetime = new ContainerLifetime();
			var newCompileMode = CompileMode.Dynamic;

			// Set new lifetime
			reg.SetLifetime(newLifetime);
			Assert.AreSame(reg.Lifetime, newLifetime);

			// Set different compile mode
			reg.SetCompileMode(newCompileMode);
			Assert.AreEqual(reg.CompileMode, newCompileMode);

			var get2 = reg.GetInstance();
			var get3 = reg.GetInstance();

			// Check that the lifetime is also being used
			Assert.AreNotSame(get1, get2);
			Assert.AreSame(get2, get3);
		}
		public void ExpressionRegistrationSetCompileModeThrowsExceptionIfInvalidCompileModeIsSupplied()
		{
			var container = new IocContainer();
			var lifetime = new TransientLifetime();
			var reg = new ExpressionRegistration<IFoo>(container, x => new Foo1(), lifetime, CompileMode.Delegate);

			reg.SetCompileMode((CompileMode)124);
		}
Exemplo n.º 4
0
        private InstanceProducer BuildMutableCollectionProducerFromControlledCollection(
            Type serviceType, Type elementType, MutableCollectionType collectionType)
        {
            Expression expression =
                this.BuildMutableCollectionExpressionFromControlledCollection(serviceType, elementType);

            // Technically, we could determine the longest lifestyle out of the elements of the collection,
            // instead of using Transient here. This would make it less likely for the user to get false
            // positive Lifestyle Mismatch warnings. Problem with that is that trying to retrieve the
            // longest lifestyle might cause the array to be cached in a way that is incorrect, because
            // who knows what kind of lifestyles the used created.
            Registration registration =
                new ExpressionRegistration(expression, serviceType, Lifestyle.Transient, this);

            return(new InstanceProducer(serviceType, registration)
            {
                IsContainerAutoRegistered = !this.GetAllInstances(elementType).Any()
            });
        }
Exemplo n.º 5
0
        private InstanceProducer BuildMutableCollectionProducerFromUncontrolledCollection(
            Type serviceType, Type elementType)
        {
            var        enumerableProducer   = this.GetRegistration(typeof(IEnumerable <>).MakeGenericType(elementType));
            Expression enumerableExpression = enumerableProducer.BuildExpression();

            var expression = Expression.Call(
                method: serviceType.IsArray
                    ? EnumerableToArrayMethod.MakeGenericMethod(elementType)
                    : EnumerableToListMethod.MakeGenericMethod(elementType),
                arg0: enumerableExpression);

            Registration registration =
                new ExpressionRegistration(expression, serviceType, Lifestyle.Transient, this);

            return(new InstanceProducer(serviceType, registration)
            {
                IsContainerAutoRegistered = true
            });
        }
        internal void AddAppliedDecorator(
            Type serviceType,
            Type decoratorType,
            Container container,
            Lifestyle lifestyle,
            Expression decoratedExpression,
            IEnumerable <KnownRelationship> decoratorRelationships = null)
        {
            var registration = new ExpressionRegistration(
                decoratedExpression, decoratorType, lifestyle, container);

            registration.ReplaceRelationships(decoratorRelationships ?? Enumerable.Empty <KnownRelationship>());

            var producer = new InstanceProducer(serviceType, registration)
            {
                IsDecorated = true
            };

            this.appliedDecorators.Add(new DecoratorInfo(decoratorType, producer));
        }
		public void ExpressionRegistrationWorksLikeExpected()
		{
			Expression<Func<IResolver, IFoo>> expression = x => new Foo1();
			var container = new IocContainer();
			var lifetime = new TransientLifetime();
			var compileMode = CompileMode.Dynamic;

			var reg = new ExpressionRegistration<IFoo>(container, expression, lifetime, compileMode);

			Assert.AreEqual(reg.ReturnType, typeof(IFoo));

			Assert.AreEqual(reg.CompileMode, compileMode);
			//Assert.AreSame(reg.Expression, expression); // Is being converted to Expression<Func<IResolver, object>>

			Assert.AreSame(reg.Lifetime, lifetime);
			
			// Test GetInstance
			var instance = reg.GetInstance();
			Assert.IsInstanceOfType(instance, typeof(Foo1));
		}
Exemplo n.º 8
0
        protected InstanceProducer CreateDecorateeFactoryProducer(
            ParameterInfo parameter, InstanceProducer decorateeProducer)
        {
            // We create a dummy expression with a null value. Much easier than passing on the real delegate.
            // We won't miss it, since the created InstanceProducer is just a dummy for purposes of analysis.
            var dummyExpression = Expression.Constant(null, parameter.ParameterType);

            var registration = new ExpressionRegistration(dummyExpression, this.Container);

            var relationship = new KnownRelationship(
                parameter.ParameterType, Lifestyle.Singleton, decorateeProducer)
            {
                // Make sure the relationship is not traversed when doing verification, because that leads to
                // false positivies.
                UseForVerification = false
            };

            registration.AddRelationship(relationship);

            return(new InstanceProducer(parameter.ParameterType, registration));
        }
Exemplo n.º 9
0
        public void ExpressionRegistrationWorksLikeExpected()
        {
            Expression <Func <IResolver, IFoo> > expression = x => new Foo1();
            var container   = new IocContainer();
            var lifetime    = new TransientLifetime();
            var compileMode = CompileMode.Dynamic;

            var reg = new ExpressionRegistration <IFoo>(container, expression, lifetime, compileMode);

            Assert.AreEqual(reg.ReturnType, typeof(IFoo));

            Assert.AreEqual(reg.CompileMode, compileMode);
            //Assert.AreSame(reg.Expression, expression); // Is being converted to Expression<Func<IResolver, object>>

            Assert.AreSame(reg.Lifetime, lifetime);

            // Test GetInstance
            var instance = reg.GetInstance();

            Assert.IsInstanceOfType(instance, typeof(Foo1));
        }
Exemplo n.º 10
0
        protected ServiceTypeDecoratorInfo GetServiceTypeInfo(Expression originalExpression,
                                                              InstanceProducer registeredProducer, Type registeredServiceType, Lifestyle lifestyle)
        {
            // registeredProducer.ServiceType and registeredServiceType are different when called by
            // container uncontrolled decorator. producer.ServiceType will be IEnumerable<T> and
            // registeredServiceType will be T.
            Func <Type, InstanceProducer> producerBuilder = implementationType =>
            {
                // The InstanceProducer created here is used to do correct diagnostics. We can't return the
                // registeredProducer here, since the lifestyle of the original producer can change after
                // the ExpressionBuilt event has ran, which means that this would invalidate the diagnostic
                // results.
                var registration =
                    new ExpressionRegistration(originalExpression, implementationType, lifestyle, this.Container);

                registration.ReplaceRelationships(registeredProducer.GetRelationships());

                return(new InstanceProducer(registeredServiceType, registration));
            };

            return(this.GetServiceTypeInfo(originalExpression, registeredProducer, producerBuilder));
        }
Exemplo n.º 11
0
        internal bool SatisfiesPredicate()
        {
            // We don't have an expression at this point, since the instances are not created by the container.
            // Therefore we fake an expression so it can still be passed on to the predicate the user might
            // have defined.
            var expression = Expression.Constant(null, this.registeredServiceType);

            var registration = new ExpressionRegistration(
                expression, this.registeredServiceType, Lifestyle.Unknown, this.Container);

            registration.ReplaceRelationships(this.e.InstanceProducer.GetRelationships());

            var info = this.GetServiceTypeInfo(
                this.e,
                originalExpression: expression,
                originalRegistration: registration,
                registeredServiceType: this.registeredServiceType);

            this.Context = this.CreatePredicateContext(this.registeredServiceType, expression, info);

            return(this.SatisfiesPredicate(this.Context));
        }
Exemplo n.º 12
0
        internal void ApplyDecorator()
        {
            var registration = new ExpressionRegistration(this.e.Expression, this.registeredServiceType,
                                                          Lifestyle.Unknown, this.Container);

            registration.ReplaceRelationships(this.e.InstanceProducer.GetRelationships());

            var serviceTypeInfo = this.GetServiceTypeInfo(this.e.Expression, this.e.InstanceProducer,
                                                          registration, this.registeredServiceType);

            Registration decoratorRegistration;

            var decoratedExpression = this.BuildDecoratorExpression(out decoratorRegistration);

            this.e.Expression = decoratedExpression;

            // Add the decorator to the list of applied decorator. This way users can use this
            // information in the predicate of the next decorator they add.
            serviceTypeInfo.AddAppliedDecorator(this.registeredServiceType, this.decoratorType,
                                                this.Container, this.Lifestyle, decoratedExpression);

            this.e.KnownRelationships.AddRange(decoratorRegistration.GetRelationships());
        }
		public void CachedLifetimeExtensionMethodThrowsExceptionIfPolicyParameterIsNull()
		{
			var container = new IocContainer();
			var reg = new ExpressionRegistration<IFoo>(container, resolver => new Foo1(), new TransientLifetime());
			LifetimeRegistrationExtensions.WithCachedLifetime(reg, null);
		}
Exemplo n.º 14
0
        private Expression HandleMethodCall(Type type, object key = null)
        {
            // Split up this method ?

            IRegistration registration;

            if (!TryGetRegistration(type, key, out registration))
            {
                // Not able to find registration referenced.
                // Could choose to ignore it and let it fail later when trying to Resolve if the type/key havent been added to the index inbetween then.

                var exceptionDescription = "Cannot Compile Registration. The registration have a reference to Type: " + type.Name;
                if (key != null)
                    exceptionDescription += " with Key: " + key;
                exceptionDescription += " which isn't registered.";

                // TODO: Throw CompileException or something ?
                throw new RegistrationException(_currentRegistration, exceptionDescription);
            }

            // Only possible to compile ExpressionRegistration's

            var expressionRegistration = registration as ExpressionRegistration;
            if (expressionRegistration != null)
            {
                // Currently the Compiler only support compiling TransientLifetime's

                if (expressionRegistration.Lifetime.GetType() == typeof(TransientLifetime))		// Check specific type incase something changes in the future and TransientLifetime is no longer sealed? in stead of using "is TransientLifetime"
                {
                    // Keep visiting and try to inline the expression
                    // Recursive call until deepest nested ExpressionRegistration with TransientLifetime is found

                    // Change current registration to new registration which should be visited and save current for later
                    var current = _currentRegistration;
                    _currentRegistration = expressionRegistration;

                    // Visit
                    var expression = this.Visit(expressionRegistration.Expression.Body);

                    // Change it back again
                    _currentRegistration = current;

                    return expression;
                }
            }

            return null;
        }
        private static InitializationContext CreateDummyInitializationContext()
        {
            var registration = new ExpressionRegistration(Expression.Constant(null), new Container());

            return(new InitializationContext(new InstanceProducer(typeof(object), registration), registration));
        }
Exemplo n.º 16
0
 public void ExpressionRegistrationThrowsExceptionIfLifetimeIsNull()
 {
     var reg = new ExpressionRegistration<object>(x => new object(), null);
 }
Exemplo n.º 17
0
 public void ExpressionRegistrationThrowsExceptionIfResolverIsNull()
 {
     var reg = new ExpressionRegistration <object>(null, x => new object(), new TransientLifetime());
 }
		public void ExpressionRegistrationThrowsExceptionIfLifetimeIsNull()
		{
			var container = new IocContainer();
			var reg = new ExpressionRegistration<object>(container, x => new object(), null);
		}
Exemplo n.º 19
0
 public void ExpressionRegistrationThrowsExceptionIfExpressionIsNull()
 {
     var container = new IocContainer();
     var reg       = new ExpressionRegistration <object>(container, null, new TransientLifetime());
 }
Exemplo n.º 20
0
        private static InitializerContext CreateDummyInitializationContext()
        {
            var registration = new ExpressionRegistration(Expression.Constant(null), new Container());

            return(new InitializerContext(registration));
        }
		public void ExpressionRegistrationThrowsExceptionIfResolverIsNull()
		{
			var reg = new ExpressionRegistration<object>(null, x => new object(), new TransientLifetime());
		}
		public void ExpressionRegistrationThrowsExceptionIfExpressionIsNull()
		{
			var container = new IocContainer();
			var reg = new ExpressionRegistration<object>(container, null, new TransientLifetime());
		}
Exemplo n.º 23
0
 public void ExpressionRegistrationThrowsExceptionIfLifetimeIsNull()
 {
     var container = new IocContainer();
     var reg       = new ExpressionRegistration <object>(container, x => new object(), null);
 }
Exemplo n.º 24
0
 public void ExpressionRegistrationThrowsExceptionIfExpressionIsNull()
 {
     var reg = new ExpressionRegistration<object>(null, new TransientLifetime());
 }