public void GetImportTypeIdentity_should_return_tyoe_identity_of_default_contract_type_of_last_default_convention_when_match_is_available()
        {
            var service =
                GetServiceWithDefaultConventions();

            service.DefaultConventions.Add(
                new TypeDefaultConvention
            {
                ContractName = string.Empty,
                ContractType = typeof(decimal),
                TargetType   = typeof(string)
            });

            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Name);

            var convention =
                new ImportConvention()
            {
                ContractName = null,
                ContractType = null,
                Members      = x => new[] { member }
            };

            var result =
                service.GetImportTypeIdentity(convention, member);

            var expectedTypeIdentity =
                AttributedModelServices.GetTypeIdentity(typeof(decimal));

            result.ShouldEqual(expectedTypeIdentity);
        }
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)");
        }
        public void GetExportContractName_should_return_default_contract_name_of_last_default_convention_when_match_is_available()
        {
            const string expectedContractName = "Bar";

            var service =
                GetServiceWithDefaultConventions();

            service.DefaultConventions.Add(
                new TypeDefaultConvention
            {
                ContractName = expectedContractName,
                ContractType = typeof(decimal),
                TargetType   = typeof(string)
            });

            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Name);

            var convention =
                new ExportConvention
            {
                ContractName = null,
                ContractType = null,
                Members      = x => new[] { member }
            };

            var result =
                service.GetExportContractName(convention, member);

            result.ShouldEqual(expectedContractName);
        }
        public FakeConventionRegistry()
        {
            this.TypeScanner = new TypeScanner();

            PartWithConvention <PartConvention>()
            .ForTypesMatching(x => true)
            .MakeNonShared()
            .AddMetadata("Foo", "Bar")
            .Imports(i => i.ImportWithConvention <ImportConvention>()
                     .ContractName("Contract")
                     .ContractType <IImportConvention>()
                     .Members(x => new[]
            {
                ReflectionServices.GetProperty <IImportConvention>(z => z.ContractName),
                ReflectionServices.GetProperty <IImportConvention>(z => z.ContractType)
            }))
            .Exports(e => e.ExportWithConvention <ExportConvention>()
                     .ContractName(x => AttributedModelServices.GetContractName(typeof(FakeExportConvention)))
                     .ContractType <IExportConvention>()
                     .Members(x => new[]
            {
                ReflectionServices.GetProperty <IExportConvention>(z => z.ContractName),
                ReflectionServices.GetProperty <IExportConvention>(z => z.ContractType)
            }));

            PartWithConvention <PartConvention>()
            .ForTypesMatching(x => x.Equals(typeof(FakePart)));

            PartWithConvention <PartConvention>()
            .ForTypesMatching(x => false);
        }
Beispiel #5
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 #6
0
        public NonEmptyRegistry()
        {
            Part()
            .ForTypesMatching(x => x.Equals(typeof(FakePart)))
            .AddMetadata(new { Foo = "Bar" })
            .MakeShared()
            .ImportConstructor()
            .Exports(x =>
            {
                x.Export()
                .Members(m => new[] { ReflectionServices.GetProperty <FakePart>(z => z.Delegate) })
                .AddMetadata(new { Name = "Delegate" });
                x.Export()
                .Members(m => new[] { ReflectionServices.GetProperty <FakePart>(z => z.Name) })
                .AddMetadata(new { Name = "Name" });
            })
            .Imports(x =>
            {
                x.Import()
                .Members(m => new[] { ReflectionServices.GetProperty <FakePart>(z => z.Delegate) })
                .RequireMetadata <string>("Name")
                .AllowDefaultValue()
                .Recomposable();
                x.Import()
                .Members(m => new[] { ReflectionServices.GetProperty <FakePart>(z => z.Name) })
                .RequireMetadata <string>("Description");
                x.Import()
                .Members(m => new[] { ReflectionServices.GetProperty <FakePart>(z => z.Values) });
            });

            Part()
            .ForTypesMatching(x => false);

            ContractService.Configure(x => {
                x.ForType <Func <string, string, object> >().ContractType <FakePart>().ContractName("Test");
                x.ForType <string>().ContractType <FakePart>().ContractName("Test");
            });

            //Scan(x => {
            //    x.ExecutingAssembly();
            //    x.Assembly
            //})

            var scanner =
                new TypeScanner();

            scanner.AddTypes(() => Assembly.GetExecutingAssembly().GetExportedTypes());

            this.TypeScanner = scanner;
        }
Beispiel #7
0
        public void ContractName_should_set_contract_name_on_convention_when_called_with_function()
        {
            this.conventionBuilder
            .ContractName(x => x.Name);

            var convention =
                this.conventionBuilder
                .GetConvention();

            var member =
                ReflectionServices.GetProperty <ExportConvention>(x => x.ContractName);

            convention.ContractName.Invoke(member).ShouldEqual(member.Name);
        }
Beispiel #8
0
        public void ContractType_should_set_contract_type_on_convention_when_called_with_function()
        {
            this.conventionBuilder
            .ContractType(x => x.DeclaringType);

            var convention =
                this.conventionBuilder
                .GetConvention();

            var property =
                ReflectionServices.GetProperty <ExportConvention>(x => x.ContractType);

            convention.ContractType.Invoke(property).ShouldBeOfType <ExportConvention>();
        }
        public void GetImportTypeIdentity_should_return_null_when_convention_contract_type_is_of_type_object()
        {
            var convention =
                new ImportConvention()
            {
                ContractName = null,
                ContractType = x => typeof(object)
            };

            var service =
                GetServiceWithoutDefaultConventions();

            var results = service.GetImportTypeIdentity(
                convention, ReflectionServices.GetProperty <FakePart>(x => x.Name));

            results.ShouldBeNull();
        }
        public void GetImportContractName_should_return_default_contract_name_when_match_is_available()
        {
            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Name);

            var convention =
                new ImportConvention()
            {
                ContractName = null,
                ContractType = null,
                Members      = x => new[] { member }
            };

            var result =
                GetServiceWithDefaultConventions().GetImportContractName(convention, member);

            result.ShouldEqual("Foo");
        }
        public void GetExportContractName_should_return_contract_name_of_property_type_when_called_with_property_and_contract_name_and_type_are_null()
        {
            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Name);

            var convention =
                new ExportConvention
            {
                ContractName = null,
                ContractType = null,
                Members      = x => new[] { member }
            };

            var results =
                this.Service.GetExportContractName(convention, member);

            var expectedContractName =
                AttributedModelServices.GetContractName(member.PropertyType);

            results.ShouldEqual(expectedContractName);
        }
        public void GetImportTypeIdentity_should_return_type_identity_of_default_contract_type_when_match_is_available()
        {
            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Name);

            var convention =
                new ImportConvention
            {
                ContractName = null,
                ContractType = null,
                Members      = x => new[] { member }
            };

            var result =
                GetServiceWithDefaultConventions().GetImportTypeIdentity(convention, member);

            var expectedTypeIdentity =
                AttributedModelServices.GetTypeIdentity(typeof(int));

            result.ShouldEqual(expectedTypeIdentity);
        }
Beispiel #13
0
        public void CreateParts_should_set_type_identity_on_import_deinition_to_contract_type_from_import_convention()
        {
            var registry =
                new NonEmptyRegistry();

            var conventions =
                registry.GetConventions();

            var partDefinitions =
                GetPartDefinitionsFromNonEmptyRegistry();

            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Delegate);

            var inspectedImportDefinition =
                partDefinitions.First().ImportDefinitions.Cast <ContractBasedImportDefinition>().Skip(2).First();

            var expectedTypeIdentity =
                registry.ContractService.GetImportTypeIdentity(conventions.First().Imports.First(), member);

            inspectedImportDefinition.RequiredTypeIdentity.ShouldEqual(expectedTypeIdentity);
        }
Beispiel #14
0
        public void CreateParts_should_set_contract_name_on_import_definition_to_contract_name_from_import_convention_using_contract_service_that_is_defined_on_registry()
        {
            var registry =
                new NonEmptyRegistry();

            var conventions =
                registry.GetConventions();

            var partDefinitions =
                GetPartDefinitionsFromNonEmptyRegistry();

            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Delegate);

            var inspectedImportDefinition =
                partDefinitions.First().ImportDefinitions.Skip(2).First();

            var expectedContractName =
                registry.ContractService.GetImportContractName(conventions.First().Imports.First(), member);

            inspectedImportDefinition.ContractName.ShouldEqual(expectedContractName);
        }
        public void GetImportTypeIdentity_should_return_type_identity_of_convention_contract_type_when_contract_type_is_not_object_or_delegate()
        {
            var contractType = typeof(string);

            var convention =
                new ImportConvention()
            {
                ContractName = null,
                ContractType = x => contractType
            };

            var service =
                GetServiceWithoutDefaultConventions();

            var results = service.GetImportTypeIdentity(
                convention, ReflectionServices.GetProperty <FakePart>(x => x.Name));

            var expectedTypeIdentity =
                AttributedModelServices.GetTypeIdentity(contractType);

            results.ShouldEqual(expectedTypeIdentity);
        }
Beispiel #16
0
        public void CreateParts_should_set_type_identity_metadata_on_export_definition_to_contract_type_from_export_convention_using_contract_service_that_is_defined_on_registry()
        {
            var registry =
                new NonEmptyRegistry();

            var conventions =
                registry.GetConventions();

            var partDefinitions =
                GetPartDefinitionsFromNonEmptyRegistry();

            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Delegate);

            var inspectedExportDefinition =
                partDefinitions.First().ExportDefinitions.First();

            var expectedMetadata =
                new KeyValuePair <string, object>(CompositionConstants.ExportTypeIdentityMetadataName,
                                                  registry.ContractService.GetExportTypeIdentity(conventions.First().Exports.First(), member));

            inspectedExportDefinition.Metadata.Contains(expectedMetadata).ShouldBeTrue();
        }
        public void GetExportTypeIdentity_should_return_type_identify_of_property_type_when_called_with_property_member_and_convention_contract_type_is_null()
        {
            var convention =
                new ExportConvention
            {
                ContractName = null,
                ContractType = null
            };

            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Name);

            var service =
                GetServiceWithoutDefaultConventions();

            var result =
                service.GetExportTypeIdentity(convention, member);

            var expectedTypeIdentity =
                AttributedModelServices.GetTypeIdentity(member.PropertyType);

            result.ShouldEqual(expectedTypeIdentity);
        }
        public void GetImportTypeIdentity_should_return_type_identity_of_invoke_method_when_convention_contract_type_is_delegate()
        {
            var member =
                ReflectionServices.GetProperty <FakePart>(x => x.Delegate);

            var convention =
                new ImportConvention()
            {
                ContractName = null,
                ContractType = null,
                Members      = x => new[] { member }
            };

            var service =
                GetServiceWithoutDefaultConventions();

            var results =
                service.GetImportTypeIdentity(convention, member);

            var expectedTypeIdentity =
                AttributedModelServices.GetTypeIdentity(member.PropertyType.GetMethod("Invoke"));

            results.ShouldEqual(expectedTypeIdentity);
        }
Beispiel #19
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();
        }