public void TestPropertyOverridesWithExternallyControlledLifeTimeManager()
        {
            TypeToInjectForPropertyOverride1 defaultInjected   = new TypeToInjectForPropertyOverride1(111);
            TypeToInjectForPropertyOverride2 overrideInjected  = new TypeToInjectForPropertyOverride2(222);
            TypeToInjectForPropertyOverride3 overrideInjected1 = new TypeToInjectForPropertyOverride3(333);

            UnityContainer container = new UnityContainer();

            container.RegisterType <ISubjectTypeToInjectForPropertyOverride, SubjectType1ToInjectForPropertyOverride>(
                new ExternallyControlledLifetimeManager(), new InjectionProperty("InjectedObject", defaultInjected));

            var result1 = container.Resolve <SubjectType1ToInjectForPropertyOverride>();
            var result2 =
                container.Resolve <SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject",
                                                                                                 overrideInjected));

            Assert.AreEqual <SubjectType1ToInjectForPropertyOverride>(result1, result2);
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInjectForPropertyOverride1));

            result1 = null;
            result2 = null;
            System.GC.Collect();

            var result3 =
                container.Resolve <SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject",
                                                                                                 overrideInjected1));

            Assert.IsInstanceOfType(result3.InjectedObject, typeof(TypeToInjectForPropertyOverride3));
        }
예제 #2
0
 private static void ResolveWithOverride2()
 {
     for (int i = 0; i < iterationCount; i++)
     {
         TypeToInjectForPropertyOverride3 overrideObject = new TypeToInjectForPropertyOverride3(333);
         var result = container.Resolve <SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject", overrideObject));
         override2InjectedObjectList.Add(result);
     }
 }
        public void CanOverrideInjectionPropertyMultipleCalls()
        {
            TypeToInjectForPropertyOverride1 defaultObject   = new TypeToInjectForPropertyOverride1(111);
            TypeToInjectForPropertyOverride2 overrideObject1 = new TypeToInjectForPropertyOverride2(222);
            TypeToInjectForPropertyOverride3 overrideObject2 = new TypeToInjectForPropertyOverride3(333);

            IUnityContainer container = new UnityContainer();

            container.RegisterType <SubjectType1ToInjectForPropertyOverride>(new InjectionProperty("InjectedObject",
                                                                                                   defaultObject));
            var result1 =
                container.Resolve <SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject",
                                                                                                 overrideObject1));
            var result2 =
                container.Resolve <SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject",
                                                                                                 overrideObject2));
            var result3 = container.Resolve <SubjectType1ToInjectForPropertyOverride>();

            Assert.IsInstanceOfType(result1.InjectedObject, typeof(TypeToInjectForPropertyOverride2));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInjectForPropertyOverride3));
            Assert.IsInstanceOfType(result3.InjectedObject, typeof(TypeToInjectForPropertyOverride1));
        }
        public void TestPropertyOverridesWithExternallyControlledLifeTimeManager()
        {
            TypeToInjectForPropertyOverride1 defaultInjected = new TypeToInjectForPropertyOverride1(111);
            TypeToInjectForPropertyOverride2 overrideInjected = new TypeToInjectForPropertyOverride2(222);
            TypeToInjectForPropertyOverride3 overrideInjected1 = new TypeToInjectForPropertyOverride3(333);

            UnityContainer container = new UnityContainer();

            container.RegisterType<ISubjectTypeToInjectForPropertyOverride, SubjectType1ToInjectForPropertyOverride>(
                new ExternallyControlledLifetimeManager(), new InjectionProperty("InjectedObject", defaultInjected));

            var result1 = container.Resolve<SubjectType1ToInjectForPropertyOverride>();
            var result2 =
                container.Resolve<SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject",
                    overrideInjected));

            Assert.AreEqual<SubjectType1ToInjectForPropertyOverride>(result1, result2);
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInjectForPropertyOverride1));

            result1 = null;
            result2 = null;
            System.GC.Collect();

            var result3 =
                container.Resolve<SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject",
                    overrideInjected1));

            Assert.IsInstanceOfType(result3.InjectedObject, typeof(TypeToInjectForPropertyOverride3));
        }
        public void CanOverrideInjectionPropertyMultipleCalls()
        {
            TypeToInjectForPropertyOverride1 defaultObject = new TypeToInjectForPropertyOverride1(111);
            TypeToInjectForPropertyOverride2 overrideObject1 = new TypeToInjectForPropertyOverride2(222);
            TypeToInjectForPropertyOverride3 overrideObject2 = new TypeToInjectForPropertyOverride3(333);

            IUnityContainer container = new UnityContainer();

            container.RegisterType<SubjectType1ToInjectForPropertyOverride>(new InjectionProperty("InjectedObject",
                defaultObject));
            var result1 =
                container.Resolve<SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject",
                    overrideObject1));
            var result2 =
                container.Resolve<SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject",
                    overrideObject2));
            var result3 = container.Resolve<SubjectType1ToInjectForPropertyOverride>();

            Assert.IsInstanceOfType(result1.InjectedObject, typeof(TypeToInjectForPropertyOverride2));
            Assert.IsInstanceOfType(result2.InjectedObject, typeof(TypeToInjectForPropertyOverride3));
            Assert.IsInstanceOfType(result3.InjectedObject, typeof(TypeToInjectForPropertyOverride1));
        }
 private static void ResolveWithOverride2()
 {
     for (int i = 0; i < iterationCount; i++)
     {
         TypeToInjectForPropertyOverride3 overrideObject = new TypeToInjectForPropertyOverride3(333);
         var result = container.Resolve<SubjectType1ToInjectForPropertyOverride>(new PropertyOverride("InjectedObject", overrideObject));
         override2InjectedObjectList.Add(result);
     }
 }