예제 #1
0
        public void TestComplexExpression()
        {
            var newGenerator = new ConstructorExpressionGenerator(ConstructorSelectors.MostSpecific);
            int numFoos = 0;
            var fooGenerator = new LambdaExpressionGenerator(() => { numFoos++; return new ServiceFoo(); });
            var singleBar = new ServiceBar(new ServiceFoo());
            var barGenerator = new ConstantExpressionGenerator(singleBar);

            newGenerator.DependencyExpressionGenerating += (sender, args) =>
                {
                    IExpressionGenerator generator;
                    if (args.DependencyType == typeof(ServiceFoo))
                        generator = fooGenerator;
                    else if (args.DependencyType == typeof(ServiceBar))
                        generator = barGenerator;
                    else
                        generator = (IExpressionGenerator)sender;

                    args.Expression = generator.GenerateExpression(args.DependencyType);
                };

            var expression = newGenerator.GenerateExpression(typeof(Root));

            var func = Expression.Lambda<Func<Root>>(expression).Compile();

            var r = func();

            Assert.IsNotNull(r);
            Assert.IsNotNull(r.Bar);
            Assert.IsNotNull(r.Foo);
            Assert.Greater(numFoos, 0);
            Assert.AreSame(r.Bar, singleBar);
            Assert.AreSame(r.Bar.Foo, singleBar.Foo);
            Assert.AreNotSame(r.Foo, r.Bar.Foo);
            Assert.AreNotSame(r.Foo, singleBar.Foo);
        }
예제 #2
0
        static TypeRegistration CreateTransientWithConstructorTypeRegistration(Type concreteType)
        {
            var generator = new ConstructorExpressionGenerator(ConstructorSelectors.MostSpecific);
            generator.DependencyExpressionGenerating +=
                (sender, args) => { args.Expression = generator.GenerateExpression(args.DependencyType); };

            return new TypeRegistration(concreteType, generator, new TransientInstanceProviderFactory());
        }
예제 #3
0
        public void TransientShouldCreateNewObjectEveryTime()
        {
            IExpressionGenerator generator = new ConstructorExpressionGenerator(ConstructorSelectors.MostSpecific);
            IInstanceProvider provider = new TransientInstanceProvider(typeof(object), generator);

            AssertTransientsAreDifferent(provider.GetInstance(), provider.GetInstance());
        }
예제 #4
0
        public void ShouldCreateExpressionOnlyOnce()
        {
            IExpressionGenerator generator = new ConstructorExpressionGenerator(ConstructorSelectors.MostSpecific);
            IInstanceProvider provider = new TransientInstanceProvider(typeof(object), generator);
            Expression expr = null;
            generator.ExpressionGenerated += (sender, args) => { expr = args.Expression; };

            provider.GetInstance();

            Assert.NotNull(expr);
            Assert.AreSame(expr, provider.Expression);
        }