Exemplo n.º 1
0
        public void CompileCanInlineRegistrationsWithContainerLifetime()
        {
            // Doesnt check that Compile actually works - just that it doesnt break anything

            using (var container = new IocContainer())
            {
                container.Register<IFoo>(x => new Foo1()).ContainerLifetime();
                container.Register<IBar>(x => new Bar1());

                var reg = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()));

                container.Compile();

                var result1 = container.Resolve<IFoo>();
                var result2 = (IFooBar)container.Resolve(reg);

                Assert.AreEqual(result1, result2.Foo);
            }
        }
Exemplo n.º 2
0
        public void CompileCanInlineInstanceRegistrations()
        {
            // Doesnt check that Compile actually works - just that it doesnt break anything

            using (var container = new IocContainer())
            {
                var i1 = new Foo1();
                var i2 = new Bar1();

                container.RegisterInstance(i1);
                container.RegisterInstance(i2);

                var reg1 = container.Register<IFooBar>(x => new FooBar(x.Resolve<Foo1>(), x.Resolve<Bar1>()));

                container.Compile();

                var result = (IFooBar)container.Resolve(reg1);

                Assert.AreEqual(i1, result.Foo);
            }
        }
Exemplo n.º 3
0
		public void CompileUsingKeyDoesntBreakUsingCompileModeDynamicWhenUsingInstanceRegistrationOrContainerLifetime()
		{
			// Test that InstanceRegistration and ContainerLifetime works using CompileMode Dynamic (should not be inlined)

			using (var container = new IocContainer(defaultCompileMode: CompileMode.Dynamic))
			{
				var barInstance = new Bar1();

				var reg1 = container.Register<IFoo, Foo1>("FirstKey").WithContainerLifetime();
				var reg2 = container.RegisterInstance<IBar>(barInstance, "SecondKey");

				var reg3 = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>("FirstKey"), x.Resolve<IBar>("SecondKey")));

				container.Compile();

				var res1 = container.Resolve<IFooBar>();
				var res2 = container.Resolve<IFooBar>();

				// Assert
				Assert.AreNotSame(res1, res2);
				Assert.AreSame(barInstance, res1.Bar);
				Assert.AreSame(res1.Foo, res2.Foo);
			}
		}
Exemplo n.º 4
0
		public void RegisterAutoWorksWithCompileModeDynamicAndInternalProtectedCtor()
		{
			using (var container = new IocContainer())
			{
				var reg1 = container.Register(typeof(IFoo), typeof(InternalProtectedFoo), compileMode: CompileMode.Dynamic, includeInternalCtor: true);
				var reg2 = container.Register(typeof(IFoo), typeof(InternalProtectedFoo), "TheKey", compileMode: CompileMode.Dynamic, includeInternalCtor: true);

				// Register as InternalProtectedFoo because default for IFoo is registered above
				var reg3 = container.Register<InternalProtectedFoo, InternalProtectedFoo>(compileMode: CompileMode.Dynamic, includeInternalCtor: true);
				var reg4 = container.Register<InternalProtectedFoo, InternalProtectedFoo>("TheKey", compileMode: CompileMode.Dynamic, includeInternalCtor: true);

				container.Compile();

				var instance1 = reg1.GetInstance();
				var instance2 = reg2.GetInstance();
				var instance3 = reg3.GetInstance();
				var instance4 = reg4.GetInstance();

				Assert.IsInstanceOfType(instance1, typeof(InternalProtectedFoo));
				Assert.IsInstanceOfType(instance2, typeof(InternalProtectedFoo));
				Assert.IsInstanceOfType(instance3, typeof(InternalProtectedFoo));
				Assert.IsInstanceOfType(instance4, typeof(InternalProtectedFoo));
			}
		}
Exemplo n.º 5
0
		public void RegisterAutoWorksWithInternalCtor()
		{
			using (var container = new IocContainer())
			{
				var reg1 = container.Register(typeof(IFoo), typeof(InternalFoo), compileMode: CompileMode.Delegate, includeInternalCtor: true);
				var reg2 = container.Register(typeof(IFoo), typeof(InternalFoo), "TheKey", compileMode: CompileMode.Delegate, includeInternalCtor: true);

				var reg3 = container.Register<InternalFoo, InternalFoo>(compileMode: CompileMode.Delegate, includeInternalCtor: true);
				var reg4 = container.Register<InternalFoo, InternalFoo>("TheKey", compileMode: CompileMode.Delegate, includeInternalCtor: true);

				container.Compile();

				var instance1 = reg1.GetInstance();
				var instance2 = reg2.GetInstance();
				var instance3 = reg3.GetInstance();
				var instance4 = reg4.GetInstance();

				Assert.IsInstanceOfType(instance1, typeof(InternalFoo));
				Assert.IsInstanceOfType(instance2, typeof(InternalFoo));
				Assert.IsInstanceOfType(instance3, typeof(InternalFoo));
				Assert.IsInstanceOfType(instance4, typeof(InternalFoo));
			}
		}
Exemplo n.º 6
0
        public void CompileWorksWithPropertyInjection()
        {
            using (var container = new IocContainer())
            {
                // Arrange
                container.Register<IFoo>(c => new Foo1());
                container.Register<IBar>(c => new Bar1());
                container.Register<IFooBar>(c => new FooBar() { Foo = c.Resolve<IFoo>(), Bar = c.Resolve<IBar>() });

                // Act
                container.Compile();

                var foo = container.Resolve<IFoo>();
                var bar = container.Resolve<IBar>();
                var foobar = container.Resolve<IFooBar>();

                // Assert
                Assert.IsNotNull(foo);
                Assert.IsNotNull(bar);
                Assert.IsNotNull(foobar);

                Assert.IsNotNull(foobar.Foo);
                Assert.IsNotNull(foobar.Bar);
            }
        }
Exemplo n.º 7
0
        public void CompileThrowsExceptionIfRegistrationsAreNotValid()
        {
            using (var container = new IocContainer())
            {
                // Arrange
                container.Register<IFoo>(c => new Foo1());
                container.Register<IBar>(x => new Bar1(), "Bar");
                container.Register<IFooBar>(x => new FooBar((IFoo)x.Resolve(typeof(IFoo)), (IBar)x.Resolve(typeof(IBar), "Bar")));
                container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()), "FooBar");

                // Act
                container.Compile();
            }
        }
Exemplo n.º 8
0
        public void CompileProtectsAgainstInfiniteLoop()
        {
            using (var container = new IocContainer())
            {
                // Arrange
                container.Register<IFoo>(c => new Foo1());
                container.Register<IBar>(x => new Bar2(x.Resolve<IFooBar>()));
                container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()));

                // Act
                container.Compile();
            }
        }
Exemplo n.º 9
0
        public void CompileDoesntBreakAnything()
        {
            using (var container = new IocContainer())
            {
                // Arrange
                container.Register<IFoo>(c => new Foo1());
                container.Register<IBar>(x => new Bar1(), "BarKey");
                container.Register<IFooBar>(y => new FooBar((IFoo)y.Resolve(typeof(IFoo)), (IBar)y.Resolve(typeof(IBar), "BarKey")));
                container.Register<IFooBar>(xyz => new FooBar(xyz.Resolve<IFoo>(), xyz.Resolve<IBar>("BarKey")), "FooBarKey");

                // Act
                container.Compile();

                var foo = container.Resolve<IFoo>();
                var bar = container.Resolve<IBar>("BarKey");
                var foobar1 = container.Resolve<IFooBar>();
                var foobar2 = container.Resolve<IFooBar>("FooBarKey");

                // Assert
                Assert.IsNotNull(foo);
                Assert.IsNotNull(bar);
                Assert.IsNotNull(foobar1);
                Assert.IsNotNull(foobar2);

                Assert.IsNotNull(foobar1.Foo, null);
                Assert.IsNotNull(foobar1.Bar, null);
                Assert.IsNotNull(foobar2.Foo, null);
                Assert.IsNotNull(foobar2.Bar, null);
            }
        }
Exemplo n.º 10
0
		public void RequestLifetimeWorksWithCompile()
		{
			using (var container = new IocContainer())
			{
				// Arrange
				var context = new FakeHttpContext("Http://fakeUrl1.com");
				var requestLifetime = new RequestLifetime(() => context, disposeOnEnd: false);	// Do not test disposeOnEnd here

				container.Register<IFoo, Foo1>();
				container.Register<IBar, Bar1>().SetLifetime(requestLifetime);
				container.Register<IFooBar, FooBar>();

				// Act
				container.Compile();

				var fooBar = container.Resolve<IFooBar>();

				// Assert
				Assert.IsNotNull(fooBar);
				Assert.IsNotNull(fooBar.Foo);
				Assert.IsNotNull(fooBar.Bar);
			}
		}
Exemplo n.º 11
0
		public void RequestLifetimeNestedWorksWithCompile()
		{
			// This doesnt work - will affect other Lifetimes that just nests the expression 
			// Need to fix the scope of the input parameter IResolver

			using (var container = new IocContainer())
			{
				// Arrange
				var context = new FakeHttpContext("Http://fakeUrl1.com");
				var requestLifetime = new RequestLifetime(() => context, disposeOnEnd: false);	// Do not test disposeOnEnd here

				container.Register<IFoo, Foo1>();
				container.Register<IBar, Bar1>().SetLifetime(requestLifetime);
				container.Register<IFooBar, FooBar>();
				container.Register<IFooBarContainer, FooBarContainer>();

				// Act
				container.Compile();

				var fooBarContainer = container.Resolve<IFooBarContainer>();

				// Assert
				Assert.IsNotNull(fooBarContainer);
				Assert.IsNotNull(fooBarContainer.FooBar);
				Assert.IsNotNull(fooBarContainer.FooBar.Foo);
				Assert.IsNotNull(fooBarContainer.FooBar.Bar);
			}
		}
Exemplo n.º 12
0
		public void CompileWorksWhenCompiledMultipleTimes()
		{
			using (var container = new IocContainer())
			{
				// Arrange
				var foo = new Foo1();
				var bar = new Bar1();
				container.RegisterInstance<IFoo>(foo);
				container.RegisterInstance<IBar>(bar);
				container.Register<IFooBar, FooBar>().WithContainerLifetime();
				container.Register<IFooBarContainer, FooBarContainer>().WithContainerLifetime();

				// Act
				container.Compile();
				container.Compile();

				var fooBarContainer = container.Resolve<IFooBarContainer>();

				// Assert
				Assert.IsNotNull(fooBarContainer);
				Assert.IsNotNull(fooBarContainer.FooBar);
				Assert.IsNotNull(fooBarContainer.FooBar.Foo);
				Assert.IsNotNull(fooBarContainer.FooBar.Bar);

				Assert.AreEqual(foo, fooBarContainer.FooBar.Foo);
				Assert.AreEqual(bar, fooBarContainer.FooBar.Bar);
			}
		}
Exemplo n.º 13
0
		public void CompileWorksWithNesting2()
		{
			using (var container = new IocContainer())
			{
				// Arrange
				container.Register<IFoo, Foo1>().SetLifetime(new TransientLifetime());
				container.Register<IBar, Bar1>().SetLifetime(new ContainerLifetime());
				container.Register<IFooBar, FooBar>().WithContainerLifetime();
				container.Register<IFooBarContainer, FooBarContainer>().WithContainerLifetime();

				// Act
				container.Compile();

				var fooBarContainer = container.Resolve<IFooBarContainer>();

				// Assert
				Assert.IsNotNull(fooBarContainer);
				Assert.IsNotNull(fooBarContainer.FooBar);
				Assert.IsNotNull(fooBarContainer.FooBar.Foo);
				Assert.IsNotNull(fooBarContainer.FooBar.Bar);
			}
		}
Exemplo n.º 14
0
		public void CompileCanInlineRegistrationsWithTransientLifetime()
		{
			// Doesnt check that Compile actually works - just that it doesnt break anything

			using (var container = new IocContainer())
			{
				container.Register<IFoo>(x => new Foo1()).WithTransientLifetime();
				container.Register<IBar>(x => new Bar1());

				var reg1 = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()));
				var reg2 = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()), "Key");

				container.Compile();

				var result1 = container.Resolve<IFoo>();
				var result2 = (IFooBar)reg1.GetInstance();
				var result3 = (IFooBar)reg2.GetInstance();
				
				Assert.AreNotEqual(result1, result2.Foo);
				Assert.AreNotEqual(result1, result3.Foo);
				Assert.AreNotEqual(result2.Foo, result3.Foo);
			}
		}