public override void PrepareBasic()
        {
            this.container = new QuickInjectContainer();

            this.RegisterDummies();
            this.RegisterStandard();
            this.RegisterComplex();
        }
        public override void Dispose()
        {
            // Allow the container and everything it references to be garbage collected.
            if (this.container == null)
            {
                return;
            }

            this.container.Dispose();
            this.container = null;
        }
예제 #3
0
        public IQuickInjectContainer CreateChildContainer()
        {
            QuickInjectContainer child;
            ExtensionImpl        childContext;

            // The child container collection and build plan visitor collection are enumerated during ClearBuildPlans and child container
            // instantiation, so we must synchronize to avoid modifying the collections during enumeration.
            lock (this.lockObj)
            {
                child        = new QuickInjectContainer(this);
                childContext = new ExtensionImpl(child);
                this.children.Add(child);
            }

            // Must happen outside the lock to avoid deadlock between callers
            this.ChildContainerCreated(this, new ChildContainerCreatedEventArgs(childContext));

            return(child);
        }
예제 #4
0
        private QuickInjectContainer(QuickInjectContainer parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }

            this.Registering           += delegate { };
            this.RegisteringInstance   += delegate { };
            this.ChildContainerCreated += delegate { };

            this.RegisterDependencyTreeListener(parent.dependencyTreeListener);
            foreach (var visitor in parent.buildPlanVisitors)
            {
                this.AddBuildPlanVisitor(visitor);
            }

            this.parentContainer = parent;
            this.extensionImpl   = this.parentContainer.extensionImpl;

            this.factoryExpressionTable.Add(UnityContainerType, Expression.Constant(this));
        }
        private QuickInjectContainer(QuickInjectContainer parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }

            this.Registering += delegate { };
            this.RegisteringInstance += delegate { };
            this.ChildContainerCreated += delegate { };

            this.RegisterDependencyTreeListener(parent.dependencyTreeListener);
            foreach (var visitor in parent.buildPlanVisitors)
            {
                this.AddBuildPlanVisitor(visitor);
            }

            this.parentContainer = parent;
            this.extensionImpl = this.parentContainer.extensionImpl;

            this.factoryExpressionTable.Add(UnityContainerType, Expression.Constant(this));
        }
예제 #6
0
        public void TwoInstancesAreNotSame()
        {
            var uc = new QuickInjectContainer();
            object obj1 = uc.Resolve<object>();
            object obj2 = uc.Resolve<object>();

            Assert.AreNotSame(obj1, obj2);
        }
예제 #7
0
        public void SynchronizedLifetimeManagerRecoveryTestWithExceptionReThrownAndRecoverCalled()
        {
            var container = new QuickInjectContainer();
            container.AddBuildPlanVisitor(new LifetimeManagerRequiresRecoveryBuildPlanVisitor());

            var lifetime = new ThrowRecordingSynchronizedLifetimeManager();
            container.RegisterType<Foo>(lifetime, new Microsoft.Practices.Unity.InjectionFactory(c =>
            {
                throw new ArgumentNullException("Foobar");
            }));

            bool exceptionWasReThrown = false;

            try
            {
                container.Resolve<Foo>();
            }
            catch (ArgumentNullException)
            {
                exceptionWasReThrown = true;
            }

            Assert.IsTrue(exceptionWasReThrown);
            Assert.IsTrue(lifetime.RecoverWasCalled);
        }
예제 #8
0
        public void TestEmpty()
        {
            var uc1 = new QuickInjectContainer();

            uc1.RegisterType<ATest>(new ContainerControlledLifetimeManager());
            uc1.RegisterType<ATest>(String.Empty, new ContainerControlledLifetimeManager());
            uc1.RegisterType<ATest>(null, new ContainerControlledLifetimeManager());

            ATest a = uc1.Resolve<ATest>();
            ATest b = uc1.Resolve<ATest>(String.Empty);
            ATest c = uc1.Resolve<ATest>((string)null);

            Assert.AreEqual(a, b);
            Assert.AreEqual(b, c);
            Assert.AreEqual(a, c);
        }
예제 #9
0
        public void ComplicatedRegistrationsWithChildContainerLifetimes2()
        {
            var container = new QuickInjectContainer();
            container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor());
            var child = container.CreateChildContainer();

            var correctInstanceForIFooResolutionFromChild = new Foo();
            var correctInstanceForFooResolutionFromChild = new SuperFoo();

            var preSetFooOnLifetime = new Foo();
            SuperFoo fooResolvedFromMainContainer = new SuperFoo();

            var lifetime = new ContainerControlledLifetimeManager();
            lifetime.SetValue(fooResolvedFromMainContainer);

            container.RegisterType<IFoo, Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => new Foo()));
            container.RegisterType<IBar, Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => correctInstanceForIFooResolutionFromChild));
            container.RegisterType<Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => fooResolvedFromMainContainer));
            container.RegisterType<SuperFoo>(lifetime);
            child.RegisterType<Foo, SuperFoo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => correctInstanceForFooResolutionFromChild));

            var f = container.Resolve<Foo>();
            var g = container.Resolve<Foo>();

            Assert.AreSame(child.Resolve<IBar>(), correctInstanceForIFooResolutionFromChild);
            Assert.AreSame(child.Resolve<IFoo>(), correctInstanceForIFooResolutionFromChild);
            Assert.AreSame(child.Resolve<Foo>(), correctInstanceForFooResolutionFromChild);
            Assert.AreSame(container.Resolve<Foo>(), fooResolvedFromMainContainer);
            Assert.AreSame(container.Resolve<SuperFoo>(), fooResolvedFromMainContainer);
        }
예제 #10
0
        public void FuncOfTTest()
        {
            var container = new QuickInjectContainer();
            container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor());

            var lifetime = new ContainerControlledLifetimeManager();
            var foo = new Foo();
            lifetime.SetValue(foo);

            container.RegisterType<IFoo>(lifetime);

            var a = container.Resolve<Func<IFoo>>();

            Assert.AreSame(a(), foo);
        }
예제 #11
0
        public void UseExternallyControlledLifetimeResolve()
        {
            IUnityContainer parentuc = new QuickInjectContainer();
            parentuc.RegisterType<UnityTestClass>(new ExternallyControlledLifetimeManager());

            UnityTestClass parentinstance = parentuc.Resolve<UnityTestClass>();
            parentinstance.Name = "Hello World Ob1";

            UnityTestClass parentinstance1 = parentuc.Resolve<UnityTestClass>();

            Assert.AreSame(parentinstance.Name, parentinstance1.Name);
        }
예제 #12
0
        public void ContainerReturnsTheSameInstanceOnTheSameThread()
        {
            IUnityContainer container = new QuickInjectContainer();

            container.RegisterType<IHaveManyGenericTypesClosed, HaveManyGenericTypesClosed>(new PerThreadLifetimeManager());

            IHaveManyGenericTypesClosed a = container.Resolve<IHaveManyGenericTypesClosed>();
            IHaveManyGenericTypesClosed b = container.Resolve<IHaveManyGenericTypesClosed>();

            Assert.AreSame(a, b);
        }
예제 #13
0
        public void RegisterWithParentAndChild()
        {
            //create unity container
            var parentuc = new QuickInjectContainer();

            //register type UnityTestClass
            parentuc.RegisterType<UnityTestClass>(new ContainerControlledLifetimeManager());

            UnityTestClass mytestparent = parentuc.Resolve<UnityTestClass>();
            mytestparent.Name = "Hello World";
            IUnityContainer childuc = parentuc.CreateChildContainer();
            childuc.RegisterType<UnityTestClass>(new ContainerControlledLifetimeManager());

            UnityTestClass mytestchild = childuc.Resolve<UnityTestClass>();

            Assert.AreNotSame(mytestparent.Name, mytestchild.Name);
        }
예제 #14
0
        public void ReregistrationOfATypeUpdatesBuildCacheInChildContainer()
        {
            var container = new QuickInjectContainer();
            container.AddBuildPlanVisitor(new LifetimeManagerRequiresRecoveryBuildPlanVisitor());

            var child = container.CreateChildContainer().CreateChildContainer();

            var foo = new Foo();

            container.RegisterType<Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => foo));
            var cfoo = child.Resolve<ConsumesFoo>();
            container.RegisterType<Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => new Foo()));

            var cfoo2 = child.Resolve<ConsumesFoo>();

            Assert.AreNotSame(cfoo.Foo, cfoo2.Foo);
        }
예제 #15
0
        public void RecursiveDependencies()
        {
            IUnityContainer uc = new QuickInjectContainer();
            object obj1 = uc.Resolve<MyDependency>();

            Assert.IsNotNull(obj1);
            Assert.IsInstanceOfType(obj1, typeof(MyDependency));
        }
예제 #16
0
        public void RegisterTypeAfterRegisterInstanceDoesNotReusePreviousInstance()
        {
            var container = new QuickInjectContainer();

            var foo = new Foo();
            var foo2 = new Foo();
            container.RegisterInstance<IFoo>(foo);

            var returnedInstance = container.Resolve<IFoo>();

            var lifetime = new ContainerControlledLifetimeManager();
            lifetime.SetValue(foo2);

            container.RegisterType<IFoo>(lifetime);
            var returnedInstance2 = container.Resolve<IFoo>();

            Assert.AreNotSame(returnedInstance, returnedInstance2);
        }
예제 #17
0
        public void LifetimeManagerWillProvideValueForAnInterfaceType()
        {
            var container = new QuickInjectContainer();
            container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor());

            var lifetime = new ContainerControlledLifetimeManager();
            var foo = new Foo();
            lifetime.SetValue(foo);

            container.RegisterType<IFoo>(lifetime);

            var a = container.Resolve<IFoo>();

            Assert.AreSame(a, foo);
        }
예제 #18
0
        public void GetObject()
        {
            var uc = new QuickInjectContainer();
            object obj = uc.Resolve<object>();

            Assert.IsNotNull(obj);
        }
예제 #19
0
        public void GeneratedCodeOnlyComputesNeededDependencies()
        {
            var a = new QuickInjectContainer();
            a.RegisterType<Foo>(new ParameterizedLambdaExpressionInjectionFactory<FooProvider, Foo>(x => x.ProvideFoo()));
            a.RegisterType<string>(new ParameterizedLambdaExpressionInjectionFactory<VOS, string>((x) => x.GROB("foo")));
            a.RegisterType<Mything>(new ParameterizedLambdaExpressionInjectionFactory<VOS, Foo, Mything>((x, y) => x.GROB("Foo", "bar")));
            a.RegisterType<FooBar>(new ParameterizedInjectionFactory<VOS, Foo, FooBar>(Func));
            a.RegisterType<XA>(new TransientLifetimeManager());
            a.RegisterType<XB>(new ContainerControlledLifetimeManager());
            a.RegisterType<XC>(new TransientLifetimeManager());

            a.AddBuildPlanVisitor(new LifetimeManagerRequiresRecoveryBuildPlanVisitor());
            a.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor());

            var ss = a.Resolve<GuidString>();

            Assert.IsTrue(XC.StaticVariable == 1);
        }
예제 #20
0
        public void TwoInterfacesMappedToSameConcreteTypeGetSameInstance()
        {
            IUnityContainer container = new QuickInjectContainer();
            container.RegisterType<IFoo, Foo>(new ContainerControlledLifetimeManager());
            container.RegisterType<IBar, Foo>();
            var foo = container.Resolve<IFoo>();
            var bar = container.Resolve<IBar>();

            Assert.AreSame(foo, bar);
        }
예제 #21
0
 public ExtensionImpl(QuickInjectContainer container, DummyPolicyList policyList)
 {
     this.container = container;
     this.policyList = policyList;
 }
예제 #22
0
        public void UseContainerControlledLifetime()
        {
            UnityTestClass obj1 = new UnityTestClass();

            obj1.Name = "InstanceObj";

            var parentuc = new QuickInjectContainer();
            parentuc.RegisterType<UnityTestClass>(new ContainerControlledLifetimeManager());

            UnityTestClass parentinstance = parentuc.Resolve<UnityTestClass>();
            parentinstance.Name = "Hello World Ob1";
            parentinstance = null;
            GC.Collect();

            UnityTestClass parentinstance1 = parentuc.Resolve<UnityTestClass>();

            Assert.AreSame("Hello World Ob1", parentinstance1.Name);
        }
 public TypeRegistrationTree(QuickInjectContainer container)
 {
     this.container = container;
 }
예제 #24
0
        public void WhenInstanceIsRegisteredAsSingletonEnsureItIsNotGarbageCollected()
        {
            ITest iTest;
            BTest objB = new BTest();

            var uc1 = new QuickInjectContainer();

            uc1.RegisterType<ITest, ATest>();
            iTest = objB;

            uc1.RegisterInstance<ITest>(iTest);

            iTest = (ITest)uc1.Resolve(typeof(ITest));
            Assert.IsNotNull(iTest);

            iTest = null;

            GC.Collect();

            iTest = (ITest)uc1.Resolve(typeof(ITest));

            Assert.IsNotNull(iTest);

            iTest = (ITest)uc1.Resolve(typeof(ITest));

            Assert.IsNotNull(iTest);
        }
예제 #25
0
        public void ChildRegistrationIsChosenWhenResolvedFromChild()
        {
            IA aParent = new A();
            IA aChild = new A();

            var parent = new QuickInjectContainer();
            parent.RegisterInstance(aParent);

            IUnityContainer child = parent.CreateChildContainer();
            child.RegisterInstance(aChild);

            Assert.IsTrue(aChild == child.Resolve<IA>());
            Assert.IsTrue(aParent == parent.Resolve<IA>());
        }
예제 #26
0
        public void ContainerReturnsDifferentInstancesOnDifferentThreads()
        {
            IUnityContainer container = new QuickInjectContainer();

            container.RegisterType<IHaveManyGenericTypesClosed, HaveManyGenericTypesClosed>(new PerThreadLifetimeManager());

            Thread t1 = new Thread(new ParameterizedThreadStart(ContainerReturnsDifferentInstancesOnDifferentThreads_ThreadProcedure));
            Thread t2 = new Thread(new ParameterizedThreadStart(ContainerReturnsDifferentInstancesOnDifferentThreads_ThreadProcedure));

            ContainerReturnsDifferentInstancesOnDifferentThreads_ThreadInformation info =
                new ContainerReturnsDifferentInstancesOnDifferentThreads_ThreadInformation(container);

            t1.Start(info);
            t2.Start(info);
            t1.Join();
            t2.Join();

            IHaveManyGenericTypesClosed a = new List<IHaveManyGenericTypesClosed>(info.ThreadResults.Values)[0];
            IHaveManyGenericTypesClosed b = new List<IHaveManyGenericTypesClosed>(info.ThreadResults.Values)[1];

            Assert.AreNotSame(a, b);
        }
예제 #27
0
        public void SingletonsAreSame()
        {
            IUnityContainer uc = new QuickInjectContainer()
                .RegisterType<object>(new ContainerControlledLifetimeManager());
            object obj1 = uc.Resolve<object>();
            object obj2 = uc.Resolve<object>();

            Assert.AreSame(obj1, obj2);
            Assert.IsInstanceOfType(obj1.GetType(), typeof(object));
        }
예제 #28
0
        public void SetLifetimeGetTwice()
        {
            IUnityContainer uc = new QuickInjectContainer();

            uc.RegisterType<A>(new ContainerControlledLifetimeManager());
            A obj = uc.Resolve<A>();
            A obj1 = uc.Resolve<A>();

            Assert.AreSame(obj, obj1);
        }
예제 #29
0
        public void DuplicateRegInParentAndChild()
        {
            IA a = new A();
            IB b = new B(a);

            var parent = new QuickInjectContainer();
            parent.RegisterInstance(a).RegisterInstance(b);

            IUnityContainer child = parent.CreateChildContainer();

            var childA = child.Resolve<IA>();
            var parentA = parent.Resolve<IA>();

            var childB = child.Resolve<IB>();
            var parentB = parent.Resolve<IB>();

            Assert.IsTrue(childA == parentA);
            Assert.IsTrue(childB == parentB);
        }
        public IUnityContainer CreateChildContainer()
        {
            QuickInjectContainer child;
            ExtensionImpl childContext;

            lock (this.lockObj)
            {
                child = new QuickInjectContainer(this);
                childContext = new ExtensionImpl(child, new DummyPolicyList());
                this.children.Add(child);
            }

            // Must happen outside the lock to avoid deadlock between callers
            this.ChildContainerCreated(this, new ChildContainerCreatedEventArgs(childContext));

            return child;
        }
예제 #31
0
        public void SetSingletonRegisterInstanceTwice()
        {
            IUnityContainer uc = new QuickInjectContainer();

            A aInstance = new A();
            uc.RegisterInstance<A>(aInstance).RegisterInstance<A>(aInstance);
            A obj = uc.Resolve<A>();
            A obj1 = uc.Resolve<A>();

            Assert.AreSame(obj, obj1);
        }
예제 #32
0
        public void SetValueCallsArePreservedWhenTransientLifetimeRemovalRuns()
        {
            var container = new QuickInjectContainer();
            container.AddBuildPlanVisitor(new TransientLifetimeRemovalBuildPlanVisitor());

            container.RegisterType<IFoo, Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => new Foo()));
            container.RegisterType<IBar, Foo>(new ContainerControlledLifetimeManager(), new Microsoft.Practices.Unity.InjectionFactory(c => new Foo()));

            var a = container.Resolve<IFoo>();
            var b = container.Resolve<IBar>();

            Assert.AreSame(a, b);
        }
예제 #33
0
 public ExtensionImpl(QuickInjectContainer container)
 {
     this.container = container;
 }