Exemplo n.º 1
0
        public void OneInstancedBindingsTypeWithDefaultConstructorSucceedsIfCircularDependencies()
        {
            var first     = new DefaultConstructorCircularDependency(null);
            var second    = new DefaultConstructorCircularDependencyOther(first);
            var container = new ExtraContainer1();

            container.Bind <BaseClass, DefaultConstructorCircularDependency>("First");
            container.Bind <BaseClass, DefaultConstructorCircularDependencyOther>("Second", second);

            var firstResolved = container.Resolve <BaseClass>("First") as DefaultConstructorCircularDependency;

            Assert.NotNull(firstResolved);
            var secondResolved = firstResolved.BaseClass as DefaultConstructorCircularDependencyOther;

            Assert.NotNull(secondResolved);
            var firstCircularResolved = secondResolved.BaseClass as DefaultConstructorCircularDependency;

            Assert.NotNull(firstCircularResolved);
            var secondShouldBePassedIn = firstCircularResolved.BaseClass;

            Assert.IsNull(secondShouldBePassedIn);

            Assert.AreNotEqual(firstResolved, firstCircularResolved);
            Assert.AreEqual(second, secondResolved);
        }
Exemplo n.º 2
0
        public void DisposeProperlyClearsContainers()
        {
            var container2 = new ExtraContainer1();

            container2.Bind <BaseClass, SubClassWithFieldDependencies>();
            container2.Bind <Interface, ConcreteInterface>("First").AsSingleton();
            container2.Bind <Interface, ConcreteInterface>();
            var abstractClass    = _container.Resolve <AbstractClass>();
            var baseClass        = container2.Resolve <BaseClass>();
            var bindings         = container2.GetBindings();
            var binding          = bindings.FirstOrDefault(t => t.Item2.TypeBoundFrom == typeof(BaseClass))?.Item2;
            var bindingInterface = bindings.FirstOrDefault(t => t.Item1 == "First")?.Item2;
            var dependencies     = binding?.GetDependencies();

            Assert.NotNull(baseClass);
            Assert.NotNull(abstractClass);
            Assert.NotNull(bindings);
            Assert.NotNull(binding);
            Assert.NotNull(bindingInterface);
            Assert.NotNull(dependencies);
            Assert.AreEqual(3, bindings.Count);
            Assert.AreEqual(2, dependencies.Count);
            Assert.IsTrue(bindingInterface.HasInstanceAvailable());
            Context.Dispose();

            Assert.Throws <Exception>(() => Context.GetContainerByType <ExtraContainer1>());
            Assert.Throws <Exception>(() => Context.GetContainerByType(typeof(ExtraContainer1)));
            Assert.Throws <Exception>(() => _container.Resolve <AbstractClass>());
            Assert.IsFalse(bindingInterface.HasInstanceAvailable());
            Assert.AreEqual(0, _container.GetBindings().Count);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public void SingletonBindingsTypeWithDefaultConstructorFailIfCircularDependencies()
        {
            var container = new ExtraContainer1();

            container.Bind <BaseClass, DefaultConstructorCircularDependency>("First").AsSingleton();
            container.Bind <BaseClass, DefaultConstructorCircularDependencyOther>("Second").AsSingleton();

            ThrowsInnerException <CircularDependencyException>(() => container.Resolve <BaseClass>("First"));
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public void BindingsOfSameTypeButDifferentCategoriesReturnDifferentObjectsForInstanced()
        {
            var container = new ExtraContainer1();

            container.Bind <Interface, ConcreteInterface>("Binding1", new ConcreteInterface());
            container.Bind <Interface, ConcreteInterface>("Binding2", new ConcreteInterface());

            var interface1 = container.Resolve <Interface>("Binding1");
            var interface2 = container.Resolve <Interface>("Binding2");

            Assert.NotNull(interface1);
            Assert.NotNull(interface2);
            Assert.AreNotEqual(interface1, interface2);
        }
Exemplo n.º 7
0
        public void BindingsGivenTheSameInstanceResolveToTheSameObject()
        {
            var instance  = new ConcreteInterface();
            var container = new ExtraContainer1();

            container.Bind <Interface, ConcreteInterface>("Binding1", instance);
            container.Bind <Interface, ConcreteInterface>("Binding2", instance);

            var interface1 = container.Resolve <Interface>("Binding1");
            var interface2 = container.Resolve <Interface>("Binding2");

            Assert.NotNull(interface1);
            Assert.NotNull(interface2);
            Assert.AreEqual(interface1, interface2);
        }
Exemplo n.º 8
0
        public void CanResolveTypeWithDefaultConstructorInInheritedClass()
        {
            var container = new ExtraContainer1();

            container.Bind <BaseClass, SubSubClassWithConstructorDependencies>();
            container.Bind <Interface, ConcreteInterface>();
            container.Bind <AbstractClass, ConcreteAbstractClass>("First");

            var baseClass = container.Resolve <BaseClass>() as SubClassWithConstructorDependencies;

            Assert.NotNull(baseClass);
            Assert.NotNull(baseClass.Interface);
            Assert.NotNull(baseClass.AbstractClass);
            Assert.AreEqual(typeof(ConcreteInterface), baseClass.Interface.GetType());
            Assert.AreEqual(typeof(ConcreteAbstractClass), baseClass.AbstractClass.GetType());
        }
Exemplo n.º 9
0
        public void CanMakeValidBindings()
        {
            Context.Dispose();
            var container = new ExtraContainer1();

            container.Bind <Interface, ConcreteInterface>();
            container.Bind <AbstractClass, ConcreteAbstractClass>();
            container.Bind <BaseClass, SubClass>();
            container.Bind <Interface, ConcreteInterface>("First");
            container.Bind <AbstractClass, ConcreteAbstractClass>("Second");
            container.Bind <BaseClass, SubClass>("Third");

            int index    = 0;
            var bindings = container.GetBindings();

            Assert.AreEqual(6, bindings.Count);
            Assert.AreEqual(string.Empty, bindings[index].Item1);
            Assert.AreSame(typeof(Interface), bindings[index].Item2.TypeBoundFrom);
            Assert.AreSame(typeof(ConcreteInterface), bindings[index].Item2.TypeBoundTo);
            Assert.AreEqual("First", bindings[++index].Item1);
            Assert.AreSame(typeof(Interface), bindings[index].Item2.TypeBoundFrom);
            Assert.AreSame(typeof(ConcreteInterface), bindings[index].Item2.TypeBoundTo);
            Assert.AreEqual(string.Empty, bindings[++index].Item1);
            Assert.AreSame(typeof(AbstractClass), bindings[index].Item2.TypeBoundFrom);
            Assert.AreSame(typeof(ConcreteAbstractClass), bindings[index].Item2.TypeBoundTo);
            Assert.AreEqual("Second", bindings[++index].Item1);
            Assert.AreSame(typeof(AbstractClass), bindings[index].Item2.TypeBoundFrom);
            Assert.AreSame(typeof(ConcreteAbstractClass), bindings[index].Item2.TypeBoundTo);
            Assert.AreEqual(string.Empty, bindings[++index].Item1);
            Assert.AreSame(typeof(BaseClass), bindings[index].Item2.TypeBoundFrom);
            Assert.AreSame(typeof(SubClass), bindings[index].Item2.TypeBoundTo);
            Assert.AreEqual("Third", bindings[++index].Item1);
            Assert.AreSame(typeof(BaseClass), bindings[index].Item2.TypeBoundFrom);
            Assert.AreSame(typeof(SubClass), bindings[index].Item2.TypeBoundTo);
        }
Exemplo n.º 10
0
        public void CanResolveTypeWithSingletonCircleDependencies()
        {
            var container = new ExtraContainer1();

            container.Bind <Interface, ConcreteInterface>();
            container.Bind <AbstractClass, ConcreteAbstractClass>();
            container.Bind <BaseClass, SubClassWithFieldDependencies>();
            container.Bind <BaseClass, SubClassWithCircularDependencies>("Circle").AsSingleton();
            container.Bind <BaseClass, OtherSubClassWithCircularDependencies>("Other").AsSingleton();
            container.Bind <Interface, ConcreteInterface>("First");
            container.Bind <AbstractClass, ConcreteAbstractClass>("Second");
            container.Bind <BaseClass, SubClassWithCategory>("Third");

            var instance = container.Resolve <BaseClass>("Circle") as SubClassWithCircularDependencies;

            Assert.NotNull(instance);

            var dependencyInterface = instance.Interface;

            Assert.NotNull(dependencyInterface);
            Assert.AreEqual(typeof(ConcreteInterface), dependencyInterface.GetType());

            var circular = instance.Circular;

            Assert.NotNull(circular);
            Assert.AreEqual(typeof(OtherSubClassWithCircularDependencies), circular.GetType());

            var otherInterface = ((OtherSubClassWithCircularDependencies)circular).Interface;

            Assert.NotNull(otherInterface);
            Assert.AreEqual(typeof(ConcreteInterface), otherInterface.GetType());

            var otherCircular = ((OtherSubClassWithCircularDependencies)circular).Circle;

            Assert.NotNull(otherCircular);
            Assert.AreEqual(typeof(SubClassWithCircularDependencies), otherCircular.GetType());
            Assert.AreEqual(instance, otherCircular);
        }