Ejemplo n.º 1
0
        public void NestedContainerOverrideInParentDoesNotReturnOverridenInChildAcrossCalls()
        {
            IToWhichDependent overrideValue = new Type2ToWhichDependent(222);

            IUnityContainer container      = new UnityContainer();
            IUnityContainer childContainer = container.CreateChildContainer();

            // Registering the parent default types.
            container.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111));

            // Overriding the default values for the parent container.
            Type1DependingOnOtherType parentResult = (Type1DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue));
            // Resolving child container to the default type.
            Type1DependingOnOtherType childResult = (Type1DependingOnOtherType)childContainer.Resolve <IDependingOnOtherType>();

            // The parent overriden values should be reflected.
            Assert.IsInstanceOfType(parentResult.IToWhichDependent, typeof(Type2ToWhichDependent));
            Assert.IsInstanceOfType(parentResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent));
            Assert.AreEqual <int>(parentResult.IToWhichDependent.X, 222);

            // The parent default registered type should be reflected, since there are no type registered exclusively for the child container.
            Assert.IsInstanceOfType(childResult.IToWhichDependent, typeof(Type1ToWhichDependent));
            Assert.IsInstanceOfType(childResult.OneMoreIToWhichDependent, typeof(Type1ToWhichDependent));
            Assert.AreEqual <int>(childResult.IToWhichDependent.X, 111);
        }
Ejemplo n.º 2
0
        public void DependencyOverrideWithNestedContainer()
        {
            IToWhichDependent defaultValue   = new Type1ToWhichDependent(111);
            IToWhichDependent overrideValue3 = new Type2ToWhichDependent(333);
            IToWhichDependent overrideValue4 = new Type2ToWhichDependent(444);

            IUnityContainer container      = new UnityContainer();
            IUnityContainer childContainer = container.CreateChildContainer();

            // Registering the parent default types.
            container.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111));
            // Registering the child default types.
            childContainer.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(222));

            // Overriding the default values for the parent container.
            Type1DependingOnOtherType parentResult = (Type1DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue3));
            // Overriding the default values for the child container.
            Type1DependingOnOtherType childResult = (Type1DependingOnOtherType)childContainer.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue4));

            // The parent overriden values should be reflected.
            Assert.IsInstanceOfType(parentResult.IToWhichDependent, typeof(Type2ToWhichDependent));
            Assert.IsInstanceOfType(parentResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent));
            Assert.AreEqual <int>(parentResult.IToWhichDependent.X, 333);

            // The child overriden values should be reflected.
            Assert.IsInstanceOfType(childResult.IToWhichDependent, typeof(Type2ToWhichDependent));
            Assert.IsInstanceOfType(childResult.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent));
            Assert.AreEqual <int>(childResult.IToWhichDependent.X, 444);
        }
Ejemplo n.º 3
0
        public void DependencyOverridesTypeMismatchTest()
        {
            IToWhichDependent defaultValue  = new Type1ToWhichDependent(111);
            IToWhichDependent overrideValue = new Type2ToWhichDependent(222);

            IUnityContainer container = new UnityContainer();

            container.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111));
            Type1DependingOnOtherType result = (Type1DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(int), overrideValue));
        }
Ejemplo n.º 4
0
        public void DependencyOverridesAllMatchTest()
        {
            IToWhichDependent defaultValue  = new Type1ToWhichDependent(111);
            IToWhichDependent overrideValue = new Type2ToWhichDependent(222);

            IUnityContainer container = new UnityContainer();

            container.RegisterType <IDependingOnOtherType, Type1DependingOnOtherType>().RegisterType <IToWhichDependent, Type1ToWhichDependent>(new InjectionConstructor(111));
            Type1DependingOnOtherType result = (Type1DependingOnOtherType)container.Resolve <IDependingOnOtherType>(new DependencyOverride(typeof(IToWhichDependent), overrideValue));

            Assert.IsInstanceOfType(result.IToWhichDependent, typeof(Type2ToWhichDependent));
            Assert.IsInstanceOfType(result.OneMoreIToWhichDependent, typeof(Type2ToWhichDependent));
        }