Example #1
0
        public void DisposedInheritedContainersNoLongerResolveBindings()
        {
            var parentContainer = new ExtraContainer1();

            parentContainer.Bind <Interface, ConcreteInterface>().AsSingleton();
            parentContainer.Bind <AbstractClass, ConcreteAbstractClass>();

            var container = new ExtraContainer2(typeof(ExtraContainer1));

            container.Bind <Interface, ConcreteInterface>().AsSingleton();

            var abstractClass   = container.Resolve <AbstractClass>();
            var parentInterface = parentContainer.Resolve <Interface>();
            var childInterface  = container.Resolve <Interface>();

            parentContainer.Dispose();

            Assert.Throws <Exception>(() => container.Resolve <AbstractClass>());
            Assert.Throws <Exception>(() => parentContainer.Resolve <Interface>());
            var childInterfaceSecond = container.Resolve <Interface>();

            Assert.NotNull(abstractClass);
            Assert.NotNull(parentInterface);
            Assert.NotNull(childInterface);
            Assert.NotNull(childInterfaceSecond);
            Assert.AreEqual(childInterface, childInterfaceSecond);
            Assert.AreNotEqual(childInterface, parentInterface);
        }
Example #2
0
        public void CanChainInheritMultipleContainers()
        {
            var parent1Container = new ExtraContainer3(typeof(ExtraContainer1));//"Parent2");

            parent1Container.Bind <Interface, ConcreteInterface>().AsSingleton();
            parent1Container.Bind <AbstractClass, ConcreteAbstractClass>().AsSingleton();

            var parent2Container = new ExtraContainer1();

            parent2Container.Bind <Interface, ConcreteInterface>().AsSingleton();
            parent2Container.Bind <AbstractClass, ConcreteAbstractClass>().AsSingleton();
            parent2Container.Bind <BaseClass, SubClass>().AsSingleton();

            var container = new ExtraContainer2(typeof(ExtraContainer3));//"Parent1");

            container.Bind <Interface, ConcreteInterface>().AsSingleton();

            var baseClass            = container.Resolve <BaseClass>();
            var parent2BaseClass     = parent2Container.Resolve <BaseClass>();
            var abstractClass        = container.Resolve <AbstractClass>();
            var parent1AbstractClass = parent1Container.Resolve <AbstractClass>();
            var parent2AbstractClass = parent2Container.Resolve <AbstractClass>();
            var parent1Interface     = parent1Container.Resolve <Interface>();
            var parent2Interface     = parent2Container.Resolve <Interface>();
            var childInterface       = container.Resolve <Interface>();

            Assert.NotNull(baseClass);
            Assert.NotNull(parent2BaseClass);
            Assert.NotNull(abstractClass);
            Assert.NotNull(parent1AbstractClass);
            Assert.NotNull(parent2AbstractClass);
            Assert.NotNull(parent1Interface);
            Assert.NotNull(parent2Interface);
            Assert.NotNull(childInterface);

            Assert.AreEqual(typeof(SubClass), baseClass.GetType());
            Assert.AreEqual(typeof(SubClass), parent2BaseClass.GetType());

            Assert.AreEqual(typeof(ConcreteAbstractClass), abstractClass.GetType());
            Assert.AreEqual(typeof(ConcreteAbstractClass), parent1AbstractClass.GetType());
            Assert.AreEqual(typeof(ConcreteAbstractClass), parent2AbstractClass.GetType());

            Assert.AreEqual(typeof(ConcreteInterface), childInterface.GetType());
            Assert.AreEqual(typeof(ConcreteInterface), parent1Interface.GetType());
            Assert.AreEqual(typeof(ConcreteInterface), parent2Interface.GetType());

            Assert.AreEqual(baseClass, parent2BaseClass);

            Assert.AreEqual(abstractClass, parent1AbstractClass);
            Assert.AreNotEqual(abstractClass, parent2AbstractClass);
            Assert.AreNotEqual(parent1AbstractClass, parent2AbstractClass);

            Assert.AreNotEqual(childInterface, parent1Interface);
            Assert.AreNotEqual(childInterface, parent2Interface);
            Assert.AreNotEqual(parent1Interface, parent2Interface);
        }