コード例 #1
0
        public void WhenOverridingWithMultiplePolymorphicTypesAndSomeDoNotHaveMatchingConstructor()
        {
            IInterfaceForTypesToInject defaultInjectedObject  = new TypeToInject1(123);
            IInterfaceForTypesToInject overrideInjectedObject = new TypeToInject2(-9999);

            IUnityContainer container = new UnityContainer();

            container.RegisterType <ISubjectTypeToInject, SubjectType1ToInject>("SubjectType1ToInject", new InjectionConstructor(defaultInjectedObject));
            container.RegisterType <ISubjectTypeToInject, SubjectType2ToInject>("SubjectType2ToInject", new InjectionConstructor(defaultInjectedObject));
            container.RegisterType <ISubjectTypeToInject, SubjectType3ToInject>("SubjectType3ToInject", new InjectionConstructor(1, "abc"));

            System.Collections.Generic.IEnumerable <ISubjectTypeToInject> overriddenList = container.ResolveAll <ISubjectTypeToInject>(new ParameterOverride("injectedObject", overrideInjectedObject));
            System.Collections.Generic.IEnumerable <ISubjectTypeToInject> defaultList    = container.ResolveAll <ISubjectTypeToInject>();

            System.Collections.Generic.List <ISubjectTypeToInject> myOverridenList = new System.Collections.Generic.List <ISubjectTypeToInject>(overriddenList);

            int injectedCount = 0;
            int count         = 0;

            foreach (var item in myOverridenList)
            {
                count++;
                if (item.InjectedObject != null && item.InjectedObject.Value == -9999)
                {
                    injectedCount++;
                }
            }

            Assert.AreEqual <int>(2, injectedCount);
            Assert.AreEqual <int>(3, count);
        }
コード例 #2
0
        public void WhenInjectingConstructorWithOverrideAsDifferentPolymorphicType()
        {
            IInterfaceForTypesToInject defaultInjectionParameter = new TypeToInject1(-1);
            IInterfaceForTypesToInject overrideInjectionParameter = new TypeToInject2(9999);
            var container = new UnityContainer()
                .RegisterType<SubjectType1ToInject>(new InjectionConstructor(defaultInjectionParameter));

            var result =
                container.Resolve<SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter));

            Assert.IsInstanceOfType(result.InjectedObject, typeof(TypeToInject2));
            Assert.AreEqual<int>(9999, result.InjectedObject.Value);
        }
コード例 #3
0
        public void WhenInjectingConstructorWithOverrideAsDifferentPolymorphicType()
        {
            IInterfaceForTypesToInject defaultInjectionParameter  = new TypeToInject1(-1);
            IInterfaceForTypesToInject overrideInjectionParameter = new TypeToInject2(9999);
            var container = new UnityContainer()
                            .RegisterType <SubjectType1ToInject>(new InjectionConstructor(defaultInjectionParameter));

            var result =
                container.Resolve <SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter));

            Assert.IsInstanceOfType(result.InjectedObject, typeof(TypeToInject2));
            Assert.AreEqual <int>(9999, result.InjectedObject.Value);
        }
コード例 #4
0
        public void WhenInjectingConstructorWithOverrideAndNoDefault()
        {
            IInterfaceForTypesToInject overrideInjectionParameter1 = new TypeToInject1(9999);
            IInterfaceForTypesToInject overrideInjectionParameter2 = new TypeToInject2(8888);
            var container = new UnityContainer()
                            .RegisterType <SubjectType1ToInject>();

            var result1 =
                container.Resolve <SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter1));

            var result2 =
                container.Resolve <SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter2));

            Assert.IsInstanceOfType(result1.InjectedObject, typeof(TypeToInject1));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInject2));
        }
コード例 #5
0
        public void CanOverrideMultipleTimes()
        {
            IInterfaceForTypesToInject defaultParameter = new TypeToInject1(9999);

            IInterfaceForTypesToInject overrideInjectionParameter1 = new TypeToInject2(9999);
            IInterfaceForTypesToInject overrideInjectionParameter2 = new TypeToInject3(8888);
            var container = new UnityContainer()
                .RegisterType<SubjectType1ToInject>(new InjectionConstructor(defaultParameter));

            var result1 =
                container.Resolve<SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter1));

            var result2 =
                container.Resolve<SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter2));
            var result3 =
                container.Resolve<SubjectType1ToInject>();

            Assert.IsInstanceOfType(result1.InjectedObject, typeof(TypeToInject2));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInject3));
        }
コード例 #6
0
        public void CanOverrideMultipleTimes()
        {
            IInterfaceForTypesToInject defaultParameter = new TypeToInject1(9999);

            IInterfaceForTypesToInject overrideInjectionParameter1 = new TypeToInject2(9999);
            IInterfaceForTypesToInject overrideInjectionParameter2 = new TypeToInject3(8888);
            var container = new UnityContainer()
                            .RegisterType <SubjectType1ToInject>(new InjectionConstructor(defaultParameter));

            var result1 =
                container.Resolve <SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter1));

            var result2 =
                container.Resolve <SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter2));
            var result3 =
                container.Resolve <SubjectType1ToInject>();

            Assert.IsInstanceOfType(result1.InjectedObject, typeof(TypeToInject2));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInject3));
        }
コード例 #7
0
        public void WhenInjectingConstructorWithContainerControllerLifeTime()
        {
            IInterfaceForTypesToInject defaultParameter            = new TypeToInject1(9999);
            IInterfaceForTypesToInject overrideInjectionParameter  = new TypeToInject2(8888);
            IInterfaceForTypesToInject overrideInjectionParameter1 = new TypeToInject3(7777);

            var container = new UnityContainer()
                            .RegisterType <SubjectType1ToInject>(new ContainerControlledLifetimeManager(), new InjectionConstructor(defaultParameter));

            var result1 =
                container.Resolve <SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter));
            var result2 =
                container.Resolve <SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter1));
            var result3 =
                container.Resolve <SubjectType1ToInject>();

            Assert.IsInstanceOfType(result1.InjectedObject, typeof(TypeToInject2));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInject2));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInject2));
        }
コード例 #8
0
        public void WhenInjectingConstructorWithOverrideAndNoDefault()
        {
            IInterfaceForTypesToInject overrideInjectionParameter1 = new TypeToInject1(9999);
            IInterfaceForTypesToInject overrideInjectionParameter2 = new TypeToInject2(8888);
            var container = new UnityContainer()
                .RegisterType<SubjectType1ToInject>();

            var result1 =
                container.Resolve<SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter1));

            var result2 =
                container.Resolve<SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter2));

            Assert.IsInstanceOfType(result1.InjectedObject, typeof(TypeToInject1));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInject2));
        }
コード例 #9
0
        public void WhenOverridingWithMultiplePolymorphicTypesAndSomeDoNotHaveMatchingConstructor()
        {
            IInterfaceForTypesToInject defaultInjectedObject = new TypeToInject1(123);
            IInterfaceForTypesToInject overrideInjectedObject = new TypeToInject2(-9999);
            
            IUnityContainer container = new UnityContainer();
            
            container.RegisterType<ISubjectTypeToInject, SubjectType1ToInject>("SubjectType1ToInject", new InjectionConstructor(defaultInjectedObject));
            container.RegisterType<ISubjectTypeToInject, SubjectType2ToInject>("SubjectType2ToInject", new InjectionConstructor(defaultInjectedObject));
            container.RegisterType<ISubjectTypeToInject, SubjectType3ToInject>("SubjectType3ToInject", new InjectionConstructor(1, "abc"));

            System.Collections.Generic.IEnumerable<ISubjectTypeToInject> overriddenList = container.ResolveAll<ISubjectTypeToInject>(new ParameterOverride("injectedObject", overrideInjectedObject));
            System.Collections.Generic.IEnumerable<ISubjectTypeToInject> defaultList = container.ResolveAll<ISubjectTypeToInject>();

            System.Collections.Generic.List<ISubjectTypeToInject> myOverridenList = new System.Collections.Generic.List<ISubjectTypeToInject>(overriddenList);

            int injectedCount = 0;
            int count = 0;
            foreach (var item in myOverridenList)
            {
                count++;
                if (item.InjectedObject != null && item.InjectedObject.Value == -9999)
                {
                    injectedCount++;
                }
            }

            Assert.AreEqual<int>(2, injectedCount);
            Assert.AreEqual<int>(3, count);
        }
コード例 #10
0
        public void WhenInjectingConstructorWithContainerControllerLifeTime()
        {
            IInterfaceForTypesToInject defaultParameter = new TypeToInject1(9999);
            IInterfaceForTypesToInject overrideInjectionParameter = new TypeToInject2(8888);
            IInterfaceForTypesToInject overrideInjectionParameter1 = new TypeToInject3(7777);
         
            var container = new UnityContainer()
                .RegisterType<SubjectType1ToInject>(new ContainerControlledLifetimeManager(), new InjectionConstructor(defaultParameter));

            var result1 =
                container.Resolve<SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter));
            var result2 =
                container.Resolve<SubjectType1ToInject>(new ParameterOverride("injectedObject", overrideInjectionParameter1));
            var result3 =
                container.Resolve<SubjectType1ToInject>();

            Assert.IsInstanceOfType(result1.InjectedObject, typeof(TypeToInject2));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInject2));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInject2));
        }