Example #1
0
        public void ReregistrationOfATypeUpdatesBuildCacheInChildContainer()
        {
            var container = new QuickInjectContainer();
            container.AddBuildPlanVisitor(new LifetimeManagerRequiresRecoveryBuildPlanVisitor());

            var child = container.CreateChildContainer().CreateChildContainer();

            var foo = new Foo();

            container.RegisterType<Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => foo));
            var cfoo = child.Resolve<ConsumesFoo>();
            container.RegisterType<Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => new Foo()));

            var cfoo2 = child.Resolve<ConsumesFoo>();

            Assert.AreNotSame(cfoo.Foo, cfoo2.Foo);
        }
Example #2
0
        public void RegisterWithParentAndChild()
        {
            //create unity container
            var parentuc = new QuickInjectContainer();

            //register type UnityTestClass
            parentuc.RegisterType<UnityTestClass>(new ContainerControlledLifetimeManager());

            UnityTestClass mytestparent = parentuc.Resolve<UnityTestClass>();
            mytestparent.Name = "Hello World";
            IUnityContainer childuc = parentuc.CreateChildContainer();
            childuc.RegisterType<UnityTestClass>(new ContainerControlledLifetimeManager());

            UnityTestClass mytestchild = childuc.Resolve<UnityTestClass>();

            Assert.AreNotSame(mytestparent.Name, mytestchild.Name);
        }
Example #3
0
        public void ComplicatedRegistrationsWithChildContainerLifetimes2()
        {
            var container = new QuickInjectContainer();
            container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor());
            var child = container.CreateChildContainer();

            var correctInstanceForIFooResolutionFromChild = new Foo();
            var correctInstanceForFooResolutionFromChild = new SuperFoo();

            var preSetFooOnLifetime = new Foo();
            SuperFoo fooResolvedFromMainContainer = new SuperFoo();

            var lifetime = new ContainerControlledLifetimeManager();
            lifetime.SetValue(fooResolvedFromMainContainer);

            container.RegisterType<IFoo, Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => new Foo()));
            container.RegisterType<IBar, Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => correctInstanceForIFooResolutionFromChild));
            container.RegisterType<Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => fooResolvedFromMainContainer));
            container.RegisterType<SuperFoo>(lifetime);
            child.RegisterType<Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => correctInstanceForFooResolutionFromChild));

            var f = container.Resolve<Foo>();
            var g = container.Resolve<Foo>();

            Assert.AreSame(child.Resolve<IBar>(), correctInstanceForIFooResolutionFromChild);
            Assert.AreSame(child.Resolve<IFoo>(), correctInstanceForIFooResolutionFromChild);
            Assert.AreSame(child.Resolve<Foo>(), correctInstanceForFooResolutionFromChild);
            Assert.AreSame(container.Resolve<Foo>(), fooResolvedFromMainContainer);
            Assert.AreSame(container.Resolve<SuperFoo>(), fooResolvedFromMainContainer);
        }
Example #4
0
        public void DuplicateRegInParentAndChild()
        {
            IA a = new A();
            IB b = new B(a);

            var parent = new QuickInjectContainer();
            parent.RegisterInstance(a).RegisterInstance(b);

            IUnityContainer child = parent.CreateChildContainer();

            var childA = child.Resolve<IA>();
            var parentA = parent.Resolve<IA>();

            var childB = child.Resolve<IB>();
            var parentB = parent.Resolve<IB>();

            Assert.IsTrue(childA == parentA);
            Assert.IsTrue(childB == parentB);
        }
Example #5
0
        public void ChildRegistrationIsChosenWhenResolvedFromChild()
        {
            IA aParent = new A();
            IA aChild = new A();

            var parent = new QuickInjectContainer();
            parent.RegisterInstance(aParent);

            IUnityContainer child = parent.CreateChildContainer();
            child.RegisterInstance(aChild);

            Assert.IsTrue(aChild == child.Resolve<IA>());
            Assert.IsTrue(aParent == parent.Resolve<IA>());
        }