Example #1
0
        public void ConventionCatalog_should_support_type_exports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .Export();

            var catalog =
                new ConventionCatalog(registry);

            var instance =
                new ConventionPart <SampleExport>();

            var batch =
                new CompositionBatch();

            batch.AddPart(instance);

            var container =
                new CompositionContainer(catalog);

            container.Compose(batch);

            instance.Imports.Count().ShouldEqual(1);
        }
        public void Parts_should_return_compsable_part_definitions_that_was_created_based_on_the_part_registries()
        {
            var catalog =
                new ConventionCatalog(new NonEmptyRegistry());

            catalog.Parts.Count().ShouldEqual(1);
        }
        public void Ctor_should_set_registry_property_when_called_with_params_array_of_registries()
        {
            var catalog =
                new ConventionCatalog(new NonEmptyRegistry());

            catalog.Registries.Count().ShouldEqual(1);
        }
Example #4
0
        public void ConventionCatalog_should_support_property_imports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .ExportProperty("TextValue", "V1");

            registry
            .Part()
            .ForType <SampleImport>()
            .Export()
            .ImportProperty("TextValue", "V1");

            var catalog =
                new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue <SampleImport>();

            exportedValue.ShouldNotBeNull();
            exportedValue.TextValue.ShouldEqual("this is some text");
        }
        public void Ctor_should_set_registry_property_when_called_with_enumerable_of_registries()
        {
            var catalog =
                new ConventionCatalog(new List<IPartRegistry<IContractService>> { new NonEmptyRegistry() });

            catalog.Registries.Count().ShouldEqual(1);
        }
Example #6
0
        public void ConventionCatalog_should_support_field_imports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .ExportField("IntValue", "V1");

            registry
            .Part()
            .ForType <SampleImport>()
            .Export()
            .ImportField("IntValue", "V1");

            var catalog =
                new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue <SampleImport>();

            exportedValue.ShouldNotBeNull();
            exportedValue.IntValue.ShouldEqual(1234);
        }
        public void ConventionCatalog_should_support_type_exports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .Export();

            var catalog =
               new ConventionCatalog(registry);

            var instance =
                new ConventionPart<SampleExport>();

            var batch =
                new CompositionBatch();
            batch.AddPart(instance);

            var container =
                new CompositionContainer(catalog);

            container.Compose(batch);

            instance.Imports.Count().ShouldEqual(1);
        }
Example #8
0
        public void Parts_should_return_compsable_part_definitions_that_was_created_based_on_the_part_registries()
        {
            var catalog =
                new ConventionCatalog(new NonEmptyRegistry());

            catalog.Parts.Count().ShouldEqual(1);
        }
Example #9
0
        public static void Start()
        {
            // Register the CompositionContainerLifetimeHttpModule HttpModule.
            // This makes sure everything is cleaned up correctly after each request.
            CompositionContainerLifetimeHttpModule.Register();

            // Create MEF catalog based on the contents of ~/bin.
            //
            // Note that any class in the referenced assemblies implementing in "IController"
            // is automatically exported to MEF. There is no need for explicit [Export] attributes
            // on ASP.NET MVC controllers. When implementing multiple constructors ensure that
            // there is one constructor marked with the [ImportingConstructor] attribute.
            TypeCatalog typeCatalog = new TypeCatalog(typeof(Tridion.Extensions.DynamicDelivery.Factories.TridionBinaryFactory2011),
                                                      typeof(Tridion.Extensions.DynamicDelivery.Factories.TridionPageFactory),
                                                      typeof(Tridion.Extensions.DynamicDelivery.Factories.TridionLinkFactory));
            DirectoryCatalog       dirCatalog     = new DirectoryCatalog(@"bin\mef");
            DirectoryCatalog       dirCatalog2    = new DirectoryCatalog(@"D:\Temp\Test");
            MvcApplicationRegistry mvcAppRegistry = null;

            try
            {
                mvcAppRegistry = new MvcApplicationRegistry();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("caught exception " + ex.Message);
            }
            ConventionCatalog conventionCatalog = new ConventionCatalog(mvcAppRegistry);

            catalog = new AggregateCatalog(
                dirCatalog,
                conventionCatalog
                ); // Note: add your own (convention)catalogs here if needed.

            // Tell MVC3 to use MEF as its dependency resolver.
            var dependencyResolver = new CompositionDependencyResolver(catalog);

            DependencyResolver.SetResolver(dependencyResolver);

            // Tell MVC3 to resolve dependencies in controllers
            ControllerBuilder.Current.SetControllerFactory(
                new DefaultControllerFactory(
                    new CompositionControllerActivator(dependencyResolver)));

            // Tell MVC3 to resolve dependencies in filters
            FilterProviders.Providers.Remove(FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider));
            FilterProviders.Providers.Add(new CompositionFilterAttributeFilterProvider(dependencyResolver));

            // Tell MVC3 to resolve dependencies in model validators
            ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType <DataAnnotationsModelValidatorProvider>().Single());
            ModelValidatorProviders.Providers.Add(
                new CompositionDataAnnotationsModelValidatorProvider(dependencyResolver));

            // Tell MVC3 to resolve model binders through MEF. Note that a model binder should be decorated
            // with [ModelBinderExport].
            ModelBinderProviders.BinderProviders.Add(
                new CompositionModelBinderProvider(dependencyResolver));
        }
        public void SimpleExport_part_is_properly_exported()
        {
            var registry = new ConfigurationPartRegistry("mef.configuration");
            var catalog = new ConventionCatalog(registry);
            var container = new CompositionContainer(catalog);

            var simpleExport = container.GetExportedValue<SimpleExport>();
            Assert.That(simpleExport, Is.Not.Null);
        }
Example #11
0
        public void Ctor_should_retrieve_registries_when_called_with_locator()
        {
            var locator = new Mock <IPartRegistryLocator>();

            var catalog =
                new ConventionCatalog(locator.Object);

            locator.Verify(x => x.GetRegistries(), Times.Once());
        }
        public void Ctor_should_retrieve_registries_when_called_with_locator()
        {
            var locator = new Mock<IPartRegistryLocator>();

            var catalog =
                new ConventionCatalog(locator.Object);

            locator.Verify(x => x.GetRegistries(), Times.Once());
        }
        public void SimpleExport_part_is_properly_exported()
        {
            var registry  = new ConfigurationPartRegistry("mef.configuration");
            var catalog   = new ConventionCatalog(registry);
            var container = new CompositionContainer(catalog);

            var simpleExport = container.GetExportedValue <SimpleExport>();

            Assert.That(simpleExport, Is.Not.Null);
        }
        public void Ctor_should_set_conventions_when_called_with_conventions_collection()
        {
            var conventions =
                new[] { new FakePartConvention() };

            var catalog =
                new ConventionCatalog(conventions, new TypeLoader());

            catalog.Conventions.Count().ShouldEqual(1);
        }
Example #15
0
        private void Init()
        {
            var conventionCatalog = new ConventionCatalog(
                new ConfigurationPartRegistry("mef.configuration"),
                new MoviePartRegistry());
            var container = new CompositionContainer(conventionCatalog);

            // Composing this part will inject MovieLister property
            container.ComposeParts(this);
        }
        public void ExportWithPropertyImport_part_is_properly_exported_and_its_imports_are_satisfied()
        {
            var registry = new ConfigurationPartRegistry("mef.configuration");
            var catalog = new ConventionCatalog(registry);
            var container = new CompositionContainer(catalog);

            var exportWithPropertyImport = container.GetExportedValue<ExportWithPropertyImport>();
            Assert.That(exportWithPropertyImport, Is.Not.Null);
            Assert.That(exportWithPropertyImport.SimpleImport, Is.Not.Null);
        }
Example #17
0
        private static IEnumerable <ComposablePartDefinition> GetPartDefinitionsFromNonEmptyRegistry()
        {
            var registry =
                new NonEmptyRegistry();

            var catalog =
                new ConventionCatalog(registry);

            return(catalog.Parts);
        }
        public void Ctor_should_set_type_loader_when_called_with_conventions_collection()
        {
            var conventions =
                new[] { new FakePartConvention() };

            var catalog =
                new ConventionCatalog(conventions, new TypeLoader());

            catalog.TypeLoader.ShouldNotBeNull();
        }
        public void SimpleExport_part_with_metadata_is_properly_exported()
        {
            var registry = new ConfigurationPartRegistry("mef.configuration");
            var catalog = new ConventionCatalog(registry);
            var container = new CompositionContainer(catalog);

            var simpleExport = container.GetExport<SimpleExportWithMetadata, ISimpleMetadata>("simple-export");
            Assert.That(simpleExport, Is.Not.Null);
            Assert.That(simpleExport.Metadata.IntValue, Is.EqualTo(1234));
            Assert.That(simpleExport.Metadata.StrValue, Is.EqualTo("some string"));
        }
        public void ExportWithPropertyImport_part_is_properly_exported_and_its_imports_are_satisfied()
        {
            var registry  = new ConfigurationPartRegistry("mef.configuration");
            var catalog   = new ConventionCatalog(registry);
            var container = new CompositionContainer(catalog);

            var exportWithPropertyImport = container.GetExportedValue <ExportWithPropertyImport>();

            Assert.That(exportWithPropertyImport, Is.Not.Null);
            Assert.That(exportWithPropertyImport.SimpleImport, Is.Not.Null);
        }
Example #21
0
 private static void AddConventionParts(Assembly moduleDirectoryAssembly,
                                        List <ComposablePartCatalog> composableParts,
                                        IEnumerable <IPartRegistryRetriever> composablePartCatalogRetrievers)
 {
     foreach (IPartRegistryRetriever composablePartCatalogRetriever in composablePartCatalogRetrievers)
     {
         var conventionCatalog =
             new ConventionCatalog(
                 composablePartCatalogRetriever.GetPartRegistryForAssembly(moduleDirectoryAssembly));
         composableParts.Add(conventionCatalog);
     }
 }
        public void Ctor_should_set_conventions_when_called_with_convention_registry_collection()
        {
            var registry =
                new Mock<IConventionRegistry<IPartConvention>>();

            registry.Setup(x => x.GetConventions()).Returns(new[] { new FakePartConvention() });

            var catalog =
                new ConventionCatalog(new[] { registry.Object }, new TypeLoader());

            catalog.Conventions.Count().ShouldEqual(1);
        }
        public void SimpleExport_part_with_metadata_is_properly_exported()
        {
            var registry  = new ConfigurationPartRegistry("mef.configuration");
            var catalog   = new ConventionCatalog(registry);
            var container = new CompositionContainer(catalog);

            var simpleExport = container.GetExport <SimpleExportWithMetadata, ISimpleMetadata>("simple-export");

            Assert.That(simpleExport, Is.Not.Null);
            Assert.That(simpleExport.Metadata.IntValue, Is.EqualTo(1234));
            Assert.That(simpleExport.Metadata.StrValue, Is.EqualTo("some string"));
        }
Example #24
0
        public void ConventionCatalog_should_import_closed_generic()
        {
            var exportConvention =
                new ExportConvention
            {
                Members      = t => new[] { t },
                ContractType = x => typeof(ConventionPart2),
            };

            var importConvention =
                new ImportConvention
            {
                Members      = t => new[] { ReflectionServices.GetProperty <ConventionPart2>(p => p.Repository) },
                ContractType = x => typeof(IRepository <string>)
            };

            var convention =
                new PartConvention();

            convention.Imports.Add(importConvention);
            convention.Exports.Add(exportConvention);
            convention.Condition = t => t == typeof(ConventionPart2);

            var model =
                new FakePartRegistry2(convention);

            // Setup container
            ConventionCatalog conventionCatalog =
                new ConventionCatalog(model);

            var typeCatalog =
                new TypeCatalog(typeof(AttributedClosedRepository), typeof(AttributedPart2));

            var aggregated =
                new AggregateCatalog(typeCatalog, conventionCatalog);

            var container =
                new CompositionContainer(aggregated);

            var part = new AttributedPart2();

            var batch =
                new CompositionBatch();

            batch.AddPart(part);

            container.Compose(batch);

            // Assert
            part.Part.ShouldNotBeNull();
            part.Part.Repository.ShouldNotBeNull();
        }
        public void Ctor_should_retrieve_conventions_from_all_registries_when_called_with_convention_registry_collection()
        {
            var registries =
                new List<Mock<IConventionRegistry<IPartConvention>>>
                {
                    new Mock<IConventionRegistry<IPartConvention>>(),
                    new Mock<IConventionRegistry<IPartConvention>>()
                };

            var catalog =
                new ConventionCatalog(registries.Select(x => x.Object), new TypeLoader());

            registries.ForEach(x => x.VerifyAll());
        }
        public void FakePart_is_exported_using_xml_configuration()
        {
            var registry = new ConfigurationPartRegistry("mef.configuration");
            var catalog = new ConventionCatalog(registry);

            var parts = new List<ComposablePartDefinition>(catalog.Parts);
            Assert.That(parts.Count, Is.Not.EqualTo(0));

            var exports = new List<ExportDefinition>(parts[0].ExportDefinitions);
            Assert.That(exports.Count, Is.EqualTo(1));

            var imports = new List<ImportDefinition>(parts[0].ImportDefinitions);
            Assert.That(imports.Count, Is.EqualTo(1));
            Assert.That(imports[0].ContractName, Is.EqualTo("somestring"));
            Assert.That(imports[0].IsRecomposable, Is.EqualTo(false));
            Assert.That(imports[0].IsPrerequisite, Is.EqualTo(false));
        }
        public void SimpleContract_part_with_metadata_is_properly_imported()
        {
            var registry = new ConfigurationPartRegistry("mef.configuration");
            var catalog = new ConventionCatalog(registry);
            var container = new CompositionContainer(catalog);

            var simpleExport = container.GetExport<SimpleContractImporter>();
            Assert.That(simpleExport, Is.Not.Null);
            Assert.That(simpleExport.Value.SimpleContracts, Is.Not.Null);
            Assert.That(simpleExport.Value.SimpleContracts.Length, Is.EqualTo(1));
            Assert.That(simpleExport.Value.SimpleContracts[0].Metadata.IntValue, Is.EqualTo(1234));
            Assert.That(simpleExport.Value.SimpleContracts[0].Metadata.StrValue, Is.EqualTo("some string"));

            Assert.That(simpleExport.Value.SimpleContractsNoMetadataInterface, Is.Not.Null);
            Assert.That(simpleExport.Value.SimpleContractsNoMetadataInterface.Length, Is.EqualTo(1));
            Assert.That(simpleExport.Value.SimpleContractsNoMetadataInterface[0].GetType(), Is.EqualTo(typeof(SimpleContract1)));
        }
        public void SimpleContract_part_with_metadata_is_properly_imported()
        {
            var registry  = new ConfigurationPartRegistry("mef.configuration");
            var catalog   = new ConventionCatalog(registry);
            var container = new CompositionContainer(catalog);

            var simpleExport = container.GetExport <SimpleContractImporter>();

            Assert.That(simpleExport, Is.Not.Null);
            Assert.That(simpleExport.Value.SimpleContracts, Is.Not.Null);
            Assert.That(simpleExport.Value.SimpleContracts.Length, Is.EqualTo(1));
            Assert.That(simpleExport.Value.SimpleContracts[0].Metadata.IntValue, Is.EqualTo(1234));
            Assert.That(simpleExport.Value.SimpleContracts[0].Metadata.StrValue, Is.EqualTo("some string"));

            Assert.That(simpleExport.Value.SimpleContractsNoMetadataInterface, Is.Not.Null);
            Assert.That(simpleExport.Value.SimpleContractsNoMetadataInterface.Length, Is.EqualTo(1));
            Assert.That(simpleExport.Value.SimpleContractsNoMetadataInterface[0].GetType(), Is.EqualTo(typeof(SimpleContract1)));
        }
Example #29
0
        public void ConventionCatalog_should_support_constructor_injection()
        {
            var catalog =
                new ConventionCatalog(new CtorRegistry());

            var instance =
                new ConventionPart<InjectedHost>();

            var batch =
                new CompositionBatch();
            batch.AddPart(instance);

            var container =
                new CompositionContainer(catalog);

            container.Compose(batch);

            instance.Imports[0].Widgets.Count().ShouldEqual(2);
        }
Example #30
0
        public void ConventionCatalog_should_support_property_exports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .ExportProperty("TextValue", "V1");
            
            var catalog =
               new ConventionCatalog(registry);
            
            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue<string>("V1");
            Assert.That(exportedValue, Is.EqualTo("this is some text"));
        }
Example #31
0
        public void ConventionCatalog_should_support_constructor_injection()
        {
            var catalog =
                new ConventionCatalog(new CtorRegistry());

            var instance =
                new ConventionPart <InjectedHost>();

            var batch =
                new CompositionBatch();

            batch.AddPart(instance);

            var container =
                new CompositionContainer(catalog);

            container.Compose(batch);

            instance.Imports[0].Widgets.Count().ShouldEqual(2);
        }
        public void FakePart_is_exported_using_xml_configuration()
        {
            var registry = new ConfigurationPartRegistry("mef.configuration");
            var catalog  = new ConventionCatalog(registry);

            var parts = new List <ComposablePartDefinition>(catalog.Parts);

            Assert.That(parts.Count, Is.Not.EqualTo(0));

            var exports = new List <ExportDefinition>(parts[0].ExportDefinitions);

            Assert.That(exports.Count, Is.EqualTo(1));

            var imports = new List <ImportDefinition>(parts[0].ImportDefinitions);

            Assert.That(imports.Count, Is.EqualTo(1));
            Assert.That(imports[0].ContractName, Is.EqualTo("somestring"));
            Assert.That(imports[0].IsRecomposable, Is.EqualTo(false));
            Assert.That(imports[0].IsPrerequisite, Is.EqualTo(false));
        }
Example #33
0
        public void ConventionCatalog_should_support_property_exports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .ExportProperty("TextValue", "V1");

            var catalog =
                new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue <string>("V1");

            Assert.That(exportedValue, Is.EqualTo("this is some text"));
        }
Example #34
0
        public void ConventionCatalog_should_support_field_exports()
        {
            var registry = new PartRegistry();

            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
            .Part()
            .ForType <SampleExport>()
            .ExportField("IntValue", "V1");

            var catalog =
                new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue <int>("V1");

            Assert.That(exportedValue, Is.EqualTo(1234));
        }
        private static IEnumerable<ComposablePartDefinition> GetPartDefinitionsFromNonEmptyRegistry()
        {
            var registry =
                new NonEmptyRegistry();

            var catalog =
                new ConventionCatalog(registry);

            return catalog.Parts;
        }
Example #36
0
        public void ConventionCatalog_should_export_conventionpart()
        {
            // Setup conventions using the semantic model
            // This is NOT the API that the user will be exposed to,
            // there will be a DSL at the front

            var exportConvention =
                new ExportConvention
            {
                Members      = t => new[] { t },
                ContractType = x => typeof(IConventionPart),
            };

            var importConvention =
                new ImportConvention
            {
                Members      = t => new[] { ReflectionServices.GetProperty <IConventionPart>(p => p.Logger) },
                ContractType = x => typeof(ILogger)
            };

            var convention =
                new PartConvention();

            convention.Imports.Add(importConvention);
            convention.Exports.Add(exportConvention);
            convention.Condition = t => t.GetInterfaces().Contains(typeof(IConventionPart));

            var exportConvention2 =
                new ExportConvention
            {
                Members      = t => new[] { typeof(NullLogger) },
                ContractType = x => typeof(ILogger),
            };

            var convention2 =
                new PartConvention();

            convention2.Exports.Add(exportConvention2);
            convention2.Condition = t => t.GetInterfaces().Contains(typeof(ILogger));

            var model =
                new FakePartRegistry2(convention, convention2);

            // Setup container
            ConventionCatalog conventionCatalog =
                new ConventionCatalog(model);

            var typeCatalog =
                new TypeCatalog(typeof(AttributedPart));

            var aggregated =
                new AggregateCatalog(typeCatalog, conventionCatalog);

            var container =
                new CompositionContainer(aggregated);

            var part = new AttributedPart();

            var batch =
                new CompositionBatch();

            batch.AddPart(part);

            container.Compose(batch);

            // Assert
            part.Part.Count().ShouldEqual(2);
            part.Part[0].Logger.ShouldNotBeNull();
            part.Part[1].Logger.ShouldNotBeNull();
        }
Example #37
0
        public void ConventionCatalog_should_support_field_exports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .ExportField("IntValue", "V1");

            var catalog =
               new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue<int>("V1");
            Assert.That(exportedValue, Is.EqualTo(1234));
        }
Example #38
0
        public void ConventionCatalog_should_support_field_imports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .ExportField("IntValue", "V1");

            registry
                .Part()
                .ForType<SampleImport>()
                .Export()
                .ImportField("IntValue", "V1");

            var catalog =
               new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue<SampleImport>();
            exportedValue.ShouldNotBeNull();
            exportedValue.IntValue.ShouldEqual(1234);
        }
Example #39
0
        public void ConventionCatalog_should_support_property_imports()
        {
            var registry = new PartRegistry();
            registry.TypeScanner = new AssemblyTypeScanner(Assembly.GetExecutingAssembly());

            registry
                .Part()
                .ForType<SampleExport>()
                .ExportProperty("TextValue", "V1");

            registry
                .Part()
                .ForType<SampleImport>()
                .Export()
                .ImportProperty("TextValue", "V1");

            var catalog =
               new ConventionCatalog(registry);

            var container =
                new CompositionContainer(catalog);

            var exportedValue = container.GetExportedValue<SampleImport>();
            exportedValue.ShouldNotBeNull();
            exportedValue.TextValue.ShouldEqual("this is some text");
        }