Exemplo n.º 1
0
        public void ConstructorInjectionWithCyclicDependenciesThrowsException()
        {
            BindingBuilder bb = new BindingBuilder();

            bb.BindTypeToNewInstances(typeof(NeedsConstructorInjectionWithCyclicDependencies_A));
            bb.BindTypeToNewInstances(typeof(NeedsConstructorInjectionWithCyclicDependencies_B));
            bb.BindTypeToNewInstances(typeof(NeedsConstructorInjectionWithCyclicDependencies_C));

            Injector injector = UniInjectUtils.CreateInjector();

            injector.AddBindings(bb);

            Assert.Throws <CyclicConstructorDependenciesException>(delegate { injector.Create <NeedsConstructorInjectionWithCyclicDependencies_A>(); });
        }
Exemplo n.º 2
0
        public void FieldInjectionFromNewInstancesOfType()
        {
            ImplWithInstanceIndex.instanceCount = 0;

            BindingBuilder bb = new BindingBuilder();

            bb.BindTypeToNewInstances(typeof(ImplWithInstanceIndex));

            Injector injector = UniInjectUtils.CreateInjector();

            injector.AddBindings(bb);

            NeedsFieldInjection needsInjection1 = injector.CreateAndInject <NeedsFieldInjection>();
            NeedsFieldInjection needsInjection2 = injector.CreateAndInject <NeedsFieldInjection>();
            NeedsFieldInjection needsInjection3 = injector.CreateAndInject <NeedsFieldInjection>();

            // Assert injection was successful
            Assert.NotNull(needsInjection1);
            Assert.NotNull(needsInjection2);
            Assert.NotNull(needsInjection3);
            // Assert that different instances were injected.
            Assert.AreEqual(1, needsInjection1.implWithInstanceCounter.instanceIndex);
            Assert.AreEqual(2, needsInjection2.implWithInstanceCounter.instanceIndex);
            Assert.AreEqual(3, needsInjection3.implWithInstanceCounter.instanceIndex);
            Assert.AreEqual(3, ImplWithInstanceIndex.instanceCount);
        }
Exemplo n.º 3
0
        public void ConstructorInjectionWithAcyclicDependencies()
        {
            BindingBuilder bb = new BindingBuilder();

            bb.BindTypeToNewInstances(typeof(NeedsConstructorInjectionWithAcyclicDependencies_A));
            bb.BindTypeToNewInstances(typeof(NeedsConstructorInjectionWithAcyclicDependencies_B));
            bb.BindTypeToNewInstances(typeof(NeedsConstructorInjectionWithAcyclicDependencies_C));

            Injector injector = UniInjectUtils.CreateInjector();

            injector.AddBindings(bb);

            NeedsConstructorInjectionWithAcyclicDependencies_A aInstance = injector.Create <NeedsConstructorInjectionWithAcyclicDependencies_A>();

            // The dependency in A (requires B) and B (requires C) must have been resolved with new instances.
            Assert.NotNull(aInstance.bInstance);
            Assert.NotNull(aInstance.bInstance.cInstance);
        }
Exemplo n.º 4
0
        public void CyclicFieldInjection()
        {
            NeedsFieldInjectionCyclic_A.instanceCount = 0;
            NeedsFieldInjectionCyclic_B.instanceCount = 0;
            NeedsFieldInjectionCyclic_C.instanceCount = 0;

            BindingBuilder bb = new BindingBuilder();

            bb.BindTypeToNewInstances(typeof(NeedsFieldInjectionCyclic_A));
            bb.BindTypeToNewInstances(typeof(NeedsFieldInjectionCyclic_B));
            bb.BindTypeToNewInstances(typeof(NeedsFieldInjectionCyclic_C));

            Injector injector = UniInjectUtils.CreateInjector();

            injector.AddBindings(bb);
            NeedsFieldInjectionCyclic_A aInstance = injector.CreateAndInject <NeedsFieldInjectionCyclic_A>();

            // The dependency in C must have been resolved with the instance of A that was created.
            Assert.NotNull(aInstance.bInstance);
            Assert.NotNull(aInstance.bInstance.cInstance);
            Assert.NotNull(aInstance.bInstance.cInstance.aInstance);
            Assert.AreEqual(aInstance.instanceIndex, aInstance.bInstance.cInstance.aInstance.instanceIndex);
        }