Example #1
0
        public void ZyanComponentWithSharedPolicyAttribute_IsRegisteredAsSingleton()
        {
            using (var cat = new ComponentCatalog())
            {
                cat.RegisterComponents(MefCatalog);

                // get component registration
                var reg = cat.GetRegistration("UniqueName_MefSample13");
                Assert.IsNotNull(reg);
                Assert.AreEqual(ActivationType.Singleton, reg.ActivationType);

                // get component instance
                var obj = cat.GetComponent("UniqueName_MefSample13") as IMefSample;
                Assert.IsNotNull(obj);
                AssertEx.IsInstanceOf <MefSample13>(obj);
                var ms13 = (MefSample13)obj;
                var guid = ms13.Guid;

                // make sure component instance is not cleaned up
                cat.CleanUpComponentInstance(reg, obj);
                Assert.AreEqual(guid, ms13.Guid);

                // make sure component instance is not disposed with component catalog
                cat.Dispose();
                Assert.AreEqual(guid, ms13.Guid);
            }
        }
Example #2
0
        public void SharedZyanComponentFromMefContainer_IsRegistered()
        {
            using (var cat = new ComponentCatalog())
            {
                cat.RegisterComponents(MefContainer);

                // get component registration
                var reg = cat.GetRegistration("UniqueName_MefSample9");
                Assert.IsNotNull(reg);
                Assert.AreEqual(ActivationType.Singleton, reg.ActivationType);

                // get component instance
                var obj = cat.GetComponent("UniqueName_MefSample9") as IMefSample;
                Assert.IsNotNull(obj);
                AssertEx.IsInstanceOf <MefSample9>(obj);
                Assert.AreNotEqual(0, MefSample9.InstanceCount);

                // make sure component instance is not cleaned up
                cat.CleanUpComponentInstance(reg, obj);
                Assert.AreNotEqual(0, MefSample9.InstanceCount);

                // make sure component instance is not disposed with catalog
                cat.Dispose();
                Assert.AreNotEqual(0, MefSample9.InstanceCount);
            }
        }
Example #3
0
        public void SingletonComponentRegisteredWithComponentInstance_IsNotDisposed()
        {
            // this component instance is externally-owned
            var disposed       = false;
            var immortalServer = new DisposableComponent {
                Handler = () => disposed = true
            };

            var cat = new ComponentCatalog();

            cat.RegisterComponent <ISampleComponent, DisposableComponent>(immortalServer);
            Assert.IsFalse(disposed);

            var instance = cat.GetComponent <ISampleComponent>();

            AssertEx.IsInstanceOf <DisposableComponent>(instance);

            var reg = cat.GetRegistration(typeof(ISampleComponent));

            cat.CleanUpComponentInstance(reg, instance);
            Assert.IsFalse(disposed);

            immortalServer.Dispose();
            Assert.IsTrue(disposed);
        }
Example #4
0
        public void SingletonComponentRegisteredWithComponentType_IsDisposed()
        {
            var disposed = false;
            var cat      = new ComponentCatalog();

            cat.RegisterComponent <ISampleComponent, DisposableComponent>(ActivationType.Singleton);
            Assert.IsFalse(disposed);

            var instance = cat.GetComponent <ISampleComponent>();

            AssertEx.IsInstanceOf <DisposableComponent>(instance);
            instance.Handler = () => disposed = true;

            var reg = cat.GetRegistration(typeof(ISampleComponent));

            cat.CleanUpComponentInstance(reg, instance);
            Assert.IsTrue(disposed);
        }
Example #5
0
        public void SingleCallComponentRegisteredWithFactoryMethod_IsDisposed()
        {
            var disposed = false;
            var cat      = new ComponentCatalog();

            cat.RegisterComponent <ISampleComponent>(() => new DisposableComponent {
                Handler = () => disposed = true
            }, ActivationType.SingleCall);
            Assert.IsFalse(disposed);

            var instance = cat.GetComponent <ISampleComponent>();

            AssertEx.IsInstanceOf <DisposableComponent>(instance);

            var reg = cat.GetRegistration(typeof(ISampleComponent));

            cat.CleanUpComponentInstance(reg, instance);
            Assert.IsTrue(disposed);
        }
Example #6
0
        public void IObjectSourceFromMefContainer_IsRegisteredAsIs()
        {
            using (var cat = new ComponentCatalog())
            {
                cat.RegisterComponents(MefContainer);

                // get component registration
                var reg = cat.GetRegistration("UniqueName_MefSample11");
                Assert.IsNotNull(reg);

                // get component instance
                var obj = cat.GetComponent("UniqueName_MefSample11") as IObjectSource;
                Assert.IsNotNull(obj);
                AssertEx.IsInstanceOf <MefSample11>(obj);
                Assert.AreEqual(1, MefSample11.InstanceCount);

                // clean up component instance
                cat.CleanUpComponentInstance(reg, obj);
                Assert.AreEqual(0, MefSample11.InstanceCount);
            }
        }
Example #7
0
        public void NamedZyanInterfaceFromMefCatalog_IsRegistered()
        {
            using (var cat = new ComponentCatalog())
            {
                cat.RegisterComponents(MefCatalog);

                // get component registration
                var reg = cat.GetRegistration("UniqueName_MefSample8");
                Assert.IsNotNull(reg);

                // get component instance
                var obj = cat.GetComponent("UniqueName_MefSample8") as IMefSample8;
                Assert.IsNotNull(obj);
                AssertEx.IsInstanceOf <MefSample8>(obj);
                Assert.AreEqual(1, MefSample8.InstanceCount);

                // clean up component instance
                cat.CleanUpComponentInstance(reg, obj);
                Assert.AreEqual(0, MefSample8.InstanceCount);
            }
        }
Example #8
0
        public void ZyanInterfaceFromFromMefContainer_IsRegistered()
        {
            using (var cat = new ComponentCatalog())
            {
                cat.RegisterComponents(MefContainer);

                // get component registration
                var reg = cat.GetRegistration(typeof(IMefSample7));
                Assert.IsNotNull(reg);

                // get component instance
                var obj = cat.GetComponent <IMefSample7>();
                Assert.IsNotNull(obj);
                AssertEx.IsInstanceOf <MefSample7>(obj);
                Assert.AreEqual(1, MefSample7.InstanceCount);

                // clean up component instance
                cat.CleanUpComponentInstance(reg, obj);
                Assert.AreEqual(0, MefSample7.InstanceCount);
            }
        }
Example #9
0
        public void ZyanComponentFromMefContainer_IsRegisteredAsSingleCall()
        {
            using (var cat = new ComponentCatalog())
            {
                cat.RegisterComponents(MefContainer);

                // get component registration
                var reg = cat.GetRegistration(typeof(IMefSample));
                Assert.IsNotNull(reg);
                Assert.AreEqual(ActivationType.SingleCall, reg.ActivationType);

                // get component instance
                var obj = cat.GetComponent <IMefSample>();
                Assert.IsNotNull(obj);
                AssertEx.IsInstanceOf <MefSample1>(obj);
                Assert.AreEqual(1, MefSample1.InstanceCount);

                // clean up component instance
                cat.CleanUpComponentInstance(reg, obj);
                Assert.AreEqual(0, MefSample1.InstanceCount);
            }
        }
Example #10
0
        public void InheritedExportFromFromMefContainer_IsRegistered()
        {
            var cat = new ComponentCatalog();

            cat.RegisterComponents(MefContainer);

            // get component registration
            var reg = cat.GetRegistration("UniqueName_MefSample5");

            Assert.IsNotNull(reg);

            // get component instance
            var obj = cat.GetComponent("UniqueName_MefSample5") as IMefSample5;

            Assert.IsNotNull(obj);
            AssertEx.IsInstanceOf <MefSample5>(obj);
            Assert.AreEqual(1, MefSample5.InstanceCount);

            // clean up component instance
            cat.CleanUpComponentInstance(reg, obj);
            Assert.AreEqual(0, MefSample5.InstanceCount);
        }
Example #11
0
        public void SingletonComponentRegisteredWithComponentInstance_IsCleanedUp()
        {
            // this component instance is created outside, but the ownership
            // is transferred to the ComponentCatalog via cleanup delegate
            var disposed     = false;
            var mortalServer = new ReleasableComponent {
                Handler = () => disposed = true
            };

            var cat = new ComponentCatalog();

            cat.RegisterComponent <ISampleComponent, ReleasableComponent>(mortalServer, v => ((ReleasableComponent)v).Release());
            Assert.IsFalse(disposed);

            var instance = cat.GetComponent <ISampleComponent>();

            AssertEx.IsInstanceOf <ReleasableComponent>(instance);

            var reg = cat.GetRegistration(typeof(ISampleComponent));

            cat.CleanUpComponentInstance(reg, instance);
            Assert.IsTrue(disposed);
        }
Example #12
0
        public void SingletonComponentRegisteredWithFactoryMethod_IsDisposed()
        {
            var disposed = false;
            var cat = new ComponentCatalog();
            cat.RegisterComponent<ISampleComponent>(() => new DisposableComponent { Handler = () => disposed = true }, ActivationType.Singleton);
            Assert.IsFalse(disposed);

            var instance = cat.GetComponent<ISampleComponent>();
            AssertEx.IsInstanceOf<DisposableComponent>(instance);

            var reg = cat.GetRegistration(typeof(ISampleComponent));
            cat.CleanUpComponentInstance(reg, instance);
            Assert.IsTrue(disposed);
        }
Example #13
0
        public void SingletonComponentRegisteredWithComponentType_IsCleanedUp()
        {
            var disposed = false;
            var cat = new ComponentCatalog();
            cat.RegisterComponent<ISampleComponent, ReleasableComponent>(
                ActivationType.Singleton, v => ((ReleasableComponent)v).Release());
            Assert.IsFalse(disposed);

            var instance = cat.GetComponent<ISampleComponent>();
            AssertEx.IsInstanceOf<ReleasableComponent>(instance);
            instance.Handler = () => disposed = true;

            var reg = cat.GetRegistration(typeof(ISampleComponent));
            cat.CleanUpComponentInstance(reg, instance);
            Assert.IsTrue(disposed);
        }
Example #14
0
        public void SingletonComponentRegisteredWithComponentInstance_IsNotDisposed()
        {
            // this component instance is externally-owned
            var disposed = false;
            using (var immortalServer = new DisposableComponent { Handler = () => disposed = true })
            using (var cat = new ComponentCatalog())
            {
                cat.RegisterComponent<ISampleComponent, DisposableComponent>(immortalServer);
                Assert.IsFalse(disposed);

                var instance = cat.GetComponent<ISampleComponent>();
                AssertEx.IsInstanceOf<DisposableComponent>(instance);

                var reg = cat.GetRegistration(typeof(ISampleComponent));
                cat.CleanUpComponentInstance(reg, instance);
                Assert.IsFalse(disposed);

                immortalServer.Dispose();
                Assert.IsTrue(disposed);
            }
        }
Example #15
0
        public void SingletonComponentRegisteredWithComponentInstance_IsCleanedUp()
        {
            // this component instance is created outside, but the ownership
            // is transferred to the ComponentCatalog via cleanup delegate
            var disposed = false;
            var mortalServer = new ReleasableComponent { Handler = () => disposed = true };

            var cat = new ComponentCatalog();
            cat.RegisterComponent<ISampleComponent, ReleasableComponent>(mortalServer, v => ((ReleasableComponent)v).Release());
            Assert.IsFalse(disposed);

            var instance = cat.GetComponent<ISampleComponent>();
            AssertEx.IsInstanceOf<ReleasableComponent>(instance);

            var reg = cat.GetRegistration(typeof(ISampleComponent));
            cat.CleanUpComponentInstance(reg, instance);
            Assert.IsTrue(disposed);
        }