Example #1
0
        public void UnityCanAccessMixedMefAndUnityComponents()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            unityContainer.RegisterType<IUnityService1, UnityService1>(
                new ContainerControlledLifetimeManager(), true);

            // Most complex test: UnityComponent2 has dependencies on both Unity
            // and MEF components while specified MEF component has further
            // dependencies on both Unity and MEF components
            var unityComponent2 = unityContainer.Resolve<UnityComponent2>();
            Assert.IsNotNull(unityComponent2);

            var mefComponent3 = unityContainer.Resolve<IMefComponent1>("MefComponent3");
            Assert.IsNotNull(mefComponent3);
            Assert.IsTrue(mefComponent3 is MefComponent3);

            var unityService1 = unityContainer.Resolve<IUnityService1>();
            Assert.IsNotNull(unityService1);

            Assert.AreSame(mefComponent3, unityComponent2.MefComponent1);
            Assert.AreSame(unityService1, unityComponent2.UnityService1);

            Assert.IsTrue(unityComponent2.MefComponent1 is MefComponent3);

            var originalMefComponent3 = (MefComponent3)unityComponent2.MefComponent1;
            Assert.IsNotNull(originalMefComponent3.MefComponent1);
            Assert.IsNotNull(originalMefComponent3.UnityService1);
        }
        public void Unity_registered_components_take_precedence_over_MEF_registered_components_if_querying_for_a_single_component_registered_in_both_containers()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var typeCatalog = new TypeCatalog(typeof (Singleton));

            // Register catalog and types
            unityContainer.RegisterCatalog(typeCatalog);
            unityContainer.RegisterType<ISingleton, Singleton>(new ContainerControlledLifetimeManager());

            // Reset count
            Singleton.Count = 0;

            Assert.That(Singleton.Count, Is.EqualTo(0));
            var singleton = unityContainer.Resolve<ISingleton>();

            Assert.That(singleton, Is.Not.Null);
            Assert.That(Singleton.Count, Is.EqualTo(1));

            var mef = unityContainer.Resolve<CompositionContainer>();
            var mefSingleton = mef.GetExportedValue<ISingleton>();

            Assert.That(Singleton.Count, Is.EqualTo(1));
            Assert.That(singleton, Is.SameAs(mefSingleton));
        }
        private static UnityContainer ConfigureMefThenUnity()
        {
            var container = new UnityContainer();
            var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            container.RegisterCatalog(catalog);
            container.RegisterType<IUnityComponent, UnityComponent>();
            container.RegisterType<ICountableUnityComponent, CountableUnityComponent>();

            return container;
        }
        private static UnityContainer ConfigureUnityThenMef()
        {
            var container = new UnityContainer();
            TypeRegistrationTrackerExtension.RegisterIfMissing(container);

            var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            container.RegisterType<IUnityComponent, UnityComponent>();
            container.RegisterType<ICountableUnityComponent, CountableUnityComponent>();
            container.RegisterCatalog(catalog);

            return container;
        }
        protected override void ConfigureContainer()
        {
            InitialiseMappings();

            var unityContainer = new UnityContainer();
            unityContainer.RegisterCatalog(AggregateCatalog);

            ServiceConfiguration.Initialise(unityContainer);

            Container = unityContainer.Resolve<CompositionContainer>();
            base.ConfigureContainer();
        }
Example #6
0
        public void UnityCanAccessCompositionContainer()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            var compositionContainer = unityContainer.Resolve<CompositionContainer>();
            Assert.IsNotNull(compositionContainer);

            var mefComponent1 = compositionContainer.GetExportedObject<IMefComponent1>();
            Assert.IsNotNull(mefComponent1);
        }
Example #7
0
        /// <summary>
        /// Main entry point to the application.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        private static void Main(string[] args)
        {
            #region Initialization

            // Create the Unity container and self-register
            var unity = new UnityContainer();
            unity.RegisterInstance<IUnityContainer>(unity);

            // Register MEF catalogs in Unity
            unity.RegisterCatalog(new AssemblyCatalog(Assembly.GetEntryAssembly()));
            unity.RegisterCatalog(new DirectoryCatalog("."));

            #endregion

            // Register unity components
            unity.RegisterType<CoreComponent>();
            unity.RegisterType<ICoreService, CoreService1>(new ContainerControlledLifetimeManager(), true);
            unity.RegisterType<ICoreService, CoreService2>("CoreService2", new ContainerControlledLifetimeManager(), true);

            // Run the "Application"
            var p = unity.Resolve<Program>();
            p.Run();
        }
        public void UnityCircularDependencyIsDetectedTest()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            // Register catalog and types
            unityContainer.RegisterCatalog(assemblyCatalog);
            unityContainer.RegisterType<UnityOnlyComponent1>();
            
            // Test
            var unityOnlyComponent1 = unityContainer.Resolve<UnityOnlyComponent1>();
            Assert.That(unityOnlyComponent1, Is.Not.Null);
        }
        public void UnityCanResolveMefComponentThatHasUnityDependenciesTest()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            // Register catalog and types
            unityContainer.RegisterCatalog(assemblyCatalog);
            unityContainer.RegisterType<IUnityOnlyComponent, UnityOnlyComponent1>();

            // Test
            var mefComponent = unityContainer.Resolve<IMefComponentWithUnityDependencies>();
            Assert.That(mefComponent, Is.Not.Null);
            Assert.That(mefComponent.MefOnlyComponent.GetType(), Is.EqualTo(typeof(MefComponent1)));
            Assert.That(mefComponent.UnityOnlyComponent.GetType(), Is.EqualTo(typeof(UnityOnlyComponent1)));
        }
Example #10
0
        public void UnityCanAccessPureMefComponents()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            var mefComponent1 = unityContainer.Resolve<IMefComponent1>();
            Assert.IsNotNull(mefComponent1);
            Assert.IsTrue(mefComponent1 is MefComponent1);

            var unityComponent1 = unityContainer.Resolve<UnityComponent1>();
            Assert.IsNotNull(unityComponent1);

            Assert.AreSame(mefComponent1, unityComponent1.MefComponent1);
        }
        public void MefCanResolveMefComponentThatHasUnityAndMefDependenciesTest()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            // Register catalog and types
            unityContainer.RegisterCatalog(assemblyCatalog);
            unityContainer.RegisterType<IUnityOnlyComponent, UnityOnlyComponent1>();

            // Test
            var container = unityContainer.Resolve<CompositionContainer>();
            var mefMixedComponent = container.GetExportedValue<MefMixedComponent>();
            Assert.That(mefMixedComponent, Is.Not.Null);
            Assert.That(mefMixedComponent.GetType(), Is.EqualTo(typeof(MefMixedComponent)));
            Assert.That(mefMixedComponent.MefComponent.GetType(), Is.EqualTo(typeof(MefComponent1)));
            Assert.That(mefMixedComponent.UnityComponent.GetType(), Is.EqualTo(typeof(UnityOnlyComponent1)));
        }
Example #12
0
        public void MefCanAccessUnityContainer()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            var compositionContainer = unityContainer.Resolve<CompositionContainer>();
            Assert.IsNotNull(compositionContainer);

            var unityContainerFromMef1 = compositionContainer.GetExportedObject<IUnityContainer>();
            Assert.IsNotNull(unityContainerFromMef1);

            var unityContainerFromMef2 = compositionContainer.GetExportedObject<IUnityContainer>();
            Assert.IsNotNull(unityContainerFromMef2);

            Assert.AreSame(unityContainerFromMef1, unityContainerFromMef2);
            Assert.AreSame(unityContainer, unityContainerFromMef2);
        }
        public void When_querying_MEF_for_a_multiple_components_registered_in_both_containers_all_instances_are_returned()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var typeCatalog = new TypeCatalog(typeof(Singleton));

            // Register catalog and types
            unityContainer.RegisterCatalog(typeCatalog);
            unityContainer.RegisterType<ISingleton, Singleton>(new ContainerControlledLifetimeManager());

            // Reset count
            Singleton.Count = 0;

            Assert.That(Singleton.Count, Is.EqualTo(0));

            var mef = unityContainer.Resolve<CompositionContainer>();
            mef.GetExportedValues<ISingleton>();
            Assert.That(Singleton.Count, Is.EqualTo(2));
        }
        public void MefDisposesMefComponentTest()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            // Register catalog and types
            unityContainer.RegisterCatalog(assemblyCatalog);
            unityContainer.RegisterType<IUnityOnlyComponent, UnityOnlyComponent1>();

            // Test
            var container = unityContainer.Resolve<CompositionContainer>();
            var mefComponentDisposable = container.GetExportedValue<MefComponentDisposable>();
            Assert.That(mefComponentDisposable, Is.Not.Null);
            Assert.That(mefComponentDisposable.GetType(), Is.EqualTo(typeof(MefComponentDisposable)));

            unityContainer.Dispose();

            Assert.IsTrue(mefComponentDisposable.Disposed);
        }
Example #15
0
        public void MefCanAccessPureUnityComponents()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            // This service will be visible to MEF
            unityContainer.RegisterType<IUnityService1, UnityService1>(
                new ContainerControlledLifetimeManager(), true);

            var compositionContainer = unityContainer.Resolve<CompositionContainer>();
            Assert.IsNotNull(compositionContainer);

            var unityService1 = compositionContainer.GetExportedObject<IUnityService1>();
            var unityService2 = compositionContainer.GetExportedObject<IUnityService1>();

            Assert.IsNotNull(unityService1);
            Assert.IsNotNull(unityService2);
            Assert.AreSame(unityService1, unityService2);
        }
        public void MefResolvesServiceRegisteredInUnityByTypeTest()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            // Register catalog and types
            unityContainer.RegisterCatalog(assemblyCatalog);

            // Registration
            unityContainer.RegisterType<IUnityOnlyComponent, UnityOnlyComponent1>(new ContainerControlledLifetimeManager());

            var container = unityContainer.Resolve<CompositionContainer>();
            var unityOnlyComponent = container.GetExportedValue<IUnityOnlyComponent>();
            var unityOnlyComponent2 = unityContainer.Resolve<IUnityOnlyComponent>();
            Assert.That(unityOnlyComponent, Is.Not.Null);
            Assert.That(unityOnlyComponent.GetType(), Is.EqualTo(typeof(UnityOnlyComponent1)));
            Assert.That(unityOnlyComponent2, Is.Not.Null);
            Assert.That(unityOnlyComponent2.GetType(), Is.EqualTo(typeof(UnityOnlyComponent1)));
            Assert.That(unityOnlyComponent, Is.EqualTo(unityOnlyComponent2));
        }
Example #17
0
        public void MefCanAccessMixedUnityAndMefComponents()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            // This service will be visible to MEF
            unityContainer.RegisterType<IUnityService1, UnityService1>(
                new ContainerControlledLifetimeManager(), true);

            var compositionContainer = unityContainer.Resolve<CompositionContainer>();
            Assert.IsNotNull(compositionContainer);

            var mefComponent3 = compositionContainer.GetExportedObject<IMefComponent1>("MefComponent3");
            Assert.IsTrue(mefComponent3 is MefComponent3);
            Assert.IsNotNull(mefComponent3);

            var originalMefComponent3 = (MefComponent3) mefComponent3;
            Assert.IsNotNull(originalMefComponent3.MefComponent1);
            Assert.IsNotNull(originalMefComponent3.UnityService1);
        }
Example #18
0
        public void UnityDoesNotSupportMefComponentsCollections()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            var mefComponents = unityContainer.ResolveAll<IMefComponent1>();
            Assert.IsNotNull(mefComponents);
            Assert.IsTrue(mefComponents.Count() == 0);
        }
Example #19
0
        public void UnityComponentsHaveMefDependenciesInjected()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            var unityComponent0 = unityContainer.Resolve<UnityComponent0>();
            Assert.IsNotNull(unityComponent0);
            Assert.IsNotNull(unityComponent0.MefComponent1);

            var mefComponent1 = unityContainer.Resolve<IMefComponent1>();
            Assert.IsNotNull(mefComponent1);
            Assert.IsTrue(mefComponent1 is MefComponent1);

            Assert.AreSame(mefComponent1, unityComponent0.MefComponent1);
        }
Example #20
0
        public void MefCanNotAccessPureUnityComponentsIfNotProperlyRegistered()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            // This service will not be visible to MEF
            unityContainer.RegisterType<IUnityService1, UnityService1>(
                new ContainerControlledLifetimeManager());

            var compositionContainer = unityContainer.Resolve<CompositionContainer>();
            Assert.IsNotNull(compositionContainer);

            // Exception here
            Assert.That(delegate
            {
                compositionContainer.GetExportedObject<IUnityService1>();
            }, Throws.TypeOf<ImportCardinalityMismatchException>());
        }
        public void UnityResolvesUnityComponentRegisteredWithoutInterfaceTest()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            // Register catalog and types
            unityContainer.RegisterCatalog(assemblyCatalog);

            // Registration
            unityContainer.RegisterType<UnityComponent3>();

            var component2 = unityContainer.Resolve<UnityComponent2>();
            Assert.That(component2, Is.Not.Null);
            Assert.That(component2.ImportedMefComponent, Is.Not.Null);
            Assert.That(component2.ImportedMefComponent.GetType(), Is.EqualTo(typeof(MefComponent2)));
            Assert.That(component2.MefComponent.GetType(), Is.EqualTo(typeof(MefComponent2)));
        }
        public void UnityCanResolveEnumerableOfTypesRegisteredInUnityAndMefEvenIfBothMefAndUnityRegisterTheSameTypeTest()
        {
            // Setup
            var unityContainer = new UnityContainer();
            unityContainer.RegisterCatalog(new AssemblyCatalog(typeof(IModule).Assembly));

            unityContainer.RegisterType<IModule, Module1>();
            unityContainer.RegisterType<IModule, Module2>("module2");

            var modules1 = unityContainer.Resolve<IEnumerable<IModule>>();
            Assert.That(modules1.Count(), Is.EqualTo(3));
            Assert.That(modules1.OfType<Module1>().Count(), Is.EqualTo(2));

            var modules2 = unityContainer.Resolve<IEnumerable<IModule>>();
            Assert.That(modules2.Count(), Is.EqualTo(3));
            Assert.That(modules1.OfType<Module1>().Count(), Is.EqualTo(2));
        }
Example #23
0
        public void UnityCannotAccessMultipleMefComponents()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            Assert.That(delegate
            {
                unityContainer.Resolve<IMefComponent2>();
            }, Throws.TypeOf<ResolutionFailedException>());
        }
        public void UnityCanResolveMefComponentRegisteredUsingAddExportedValueTest()
        {
            MefSingletonComponent.Counter = 0;

            // Setup
            var unityContainer = new UnityContainer();
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            // Register catalog and types
            unityContainer.RegisterCatalog(assemblyCatalog);
            var compositionContainer = unityContainer.Configure<CompositionIntegration>().CompositionContainer;
            var batch = new CompositionBatch();
            var singletonComponent = new MefSingletonComponent();

            batch.AddExportedValue(singletonComponent);
            compositionContainer.Compose(batch);

            var singletonComponent1 = compositionContainer.GetExport<MefSingletonComponent>().Value;
            Assert.That(MefSingletonComponent.Counter, Is.EqualTo(1));
            Assert.That(singletonComponent1, Is.SameAs(singletonComponent));

            var singletonComponent2 = unityContainer.Resolve<MefSingletonComponent>();
            Assert.That(MefSingletonComponent.Counter, Is.EqualTo(1));
            Assert.That(singletonComponent2, Is.SameAs(singletonComponent));
        }
Example #25
0
        public void UnityComponentsDontHaveMefDependenciesInjectedIfMarkedWithPartNotComposableAttribute()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            var unityComponent00 = unityContainer.Resolve<UnityComponent00>();
            Assert.IsNotNull(unityComponent00);

            Assert.IsNull(unityComponent00.MefComponent1);
        }
        public void UnityContainerCanBeResolvedByMefTest()
        {
            // Setup
            var unityContainer = new UnityContainer();
            var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            // Register catalog and types
            unityContainer.RegisterCatalog(assemblyCatalog);

            var compositionContainer1 = unityContainer.Resolve<CompositionContainer>();
            var compositionContainer2 = unityContainer.Resolve<CompositionContainer>();
            Assert.That(compositionContainer1, Is.Not.Null);
            Assert.That(compositionContainer2, Is.Not.Null);
            Assert.That(compositionContainer1, Is.SameAs(compositionContainer2));

            var unityContainerFromMef1 = compositionContainer1.GetExportedValue<IUnityContainer>();
            var unityContainerFromMef2 = compositionContainer1.GetExportedValue<IUnityContainer>();
            
            Assert.That(unityContainerFromMef1, Is.Not.Null);
            Assert.That(unityContainerFromMef2, Is.Not.Null);
            Assert.AreSame(unityContainerFromMef1, unityContainerFromMef2);
            Assert.AreSame(unityContainer, unityContainerFromMef1);
        }
Example #27
0
        public void MefComponentsDontHaveUnityDependenciesInjectedUsingInjectionMethod()
        {
            var unityContainer = new UnityContainer();

            // Register MEF catalog in Unity
            unityContainer.RegisterCatalog(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            unityContainer.RegisterType<IUnityService1, UnityService1>(true);
            unityContainer.RegisterType<IUnityService1, UnityService2>("UnityService2", true);

            var compositionContainer = unityContainer.Resolve<CompositionContainer>();
            Assert.IsNotNull(compositionContainer);

            var mefComponent3 = compositionContainer.GetExportedObject<IMefComponent1>("MefComponent3");
            Assert.IsTrue(mefComponent3 is MefComponent3);
            Assert.IsNotNull(mefComponent3);

            var originalMefComponent3 = (MefComponent3)mefComponent3;
            Assert.IsNotNull(originalMefComponent3.MefComponent1);
            Assert.IsNotNull(originalMefComponent3.UnityService1);

            // Limited functionality
            Assert.IsNull(originalMefComponent3.UnityService1_1);
        }