Beispiel #1
0
        public void CreateParts_should_properly_extract_closed_generic_constructor_argument_when_importing_collection_and_when_contract_type_is_not_specified()
        {
            var exportConvention =
                new ExportConvention
            {
                Members      = t => new[] { t },
                ContractType = x => typeof(ConventionPart4),
            };

            var importConvention =
                new ImportConvention
            {
                Members = t => new[] { typeof(ConventionPart4).GetConstructors().Single() }
            };

            var convention =
                new PartConvention();

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

            var registry =
                new FakePartRegistry2(convention);
            ConventionPartCreator creator = new ConventionPartCreator(registry);
            var partDefinition            = creator.CreateParts().First();
            var importDefinitin           = partDefinition.ImportDefinitions.Single();

            importDefinitin.ContractName.ShouldEqual("MefContrib.Hosting.Conventions.Tests.IRepository(System.String)");
        }
Beispiel #2
0
        public void CreateParts_should_properly_extract_closed_generic_type_parameter()
        {
            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 registry =
                new FakePartRegistry2(convention);
            ConventionPartCreator creator = new ConventionPartCreator(registry);
            var partDefinition            = creator.CreateParts().First();

            partDefinition.ImportDefinitions.Single().ContractName.ShouldEqual("MefContrib.Hosting.Conventions.Tests.IRepository(System.String)");
        }
Beispiel #3
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();
        }
Beispiel #4
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();
        }