Exemplo n.º 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);
        }
Exemplo n.º 2
0
 public Root(ServiceFoo foo, ServiceBar bar)
 {
     Foo = foo;
     Bar = bar;
 }