public void FilterBasedOnMetadataUsingContainsMetadataTest() { var catalog = new AssemblyCatalog(typeof(FilteringCatalogTests).Assembly); var filteredCatalog = new FilteringCatalog(catalog, new ContainsMetadata("key", "value")); var container = new CompositionContainer(filteredCatalog); var components = container.GetExports<IMetadataComponent>(); Assert.That(components, Is.Not.Null); Assert.That(components.Count(), Is.EqualTo(1)); }
public AnotherRequest(ComposablePartCatalog parentCatalog, CompositionContainer parentContainer) { Console.WriteLine("/* AnotherRequest */"); // Create filtering catalog with creation policy filter var filteringCatalog = new FilteringCatalog( parentCatalog, new HasCreationPolicy(CreationPolicy.NonShared)); // Create the child container this.requestContainer = new CompositionContainer(filteringCatalog, parentContainer); }
public void FilterBasedOnSharedLifetimeUsingHasCreationPolicyTest() { var catalog = new AssemblyCatalog(typeof(FilteringCatalogTests).Assembly); var filteredCatalog = new FilteringCatalog(catalog, new HasCreationPolicy(CreationPolicy.Shared)); var container = new CompositionContainer(filteredCatalog); var components = container.GetExports<ILifetimeComponent>(); Assert.That(components, Is.Not.Null); Assert.That(components.Count(), Is.EqualTo(1)); Assert.That(components.First().Value.GetType(), Is.EqualTo(typeof(LifetimeComponent2))); }
public void FilterBasedOnMetadataUsingLambdaExpressionTest() { var catalog = new AssemblyCatalog(typeof(FilteringCatalogTests).Assembly); var filteredCatalog = new FilteringCatalog(catalog, partDefinition => partDefinition.Metadata.ContainsKey("key") && partDefinition.Metadata["key"].Equals("value")); var container = new CompositionContainer(filteredCatalog); var components = container.GetExports<IMetadataComponent>(); Assert.That(components, Is.Not.Null); Assert.That(components.Count(), Is.EqualTo(1)); }
public void FilterBasedOnAnyLifetimeUsingHasCreationPolicyTest() { var catalog = new AssemblyCatalog(typeof(FilteringCatalogTests).Assembly); var filteredCatalog = new FilteringCatalog(catalog, new HasCreationPolicy(CreationPolicy.Any)); var container = new CompositionContainer(filteredCatalog); var components = container.GetExports<ILifetimeComponent>(); Assert.That(components, Is.Not.Null); Assert.That(components.Count(), Is.EqualTo(2)); Assert.That(components.Select(t => t.Value).OfType<LifetimeComponent1>().First().GetType(), Is.EqualTo(typeof(LifetimeComponent1))); Assert.That(components.Select(t => t.Value).OfType<LifetimeComponent4>().First().GetType(), Is.EqualTo(typeof(LifetimeComponent4))); }
public YetAnotherRequest(ComposablePartCatalog parentCatalog, CompositionContainer parentContainer) { Console.WriteLine("/* YetAnotherRequest */"); // Create filtering catalog with "PER-REQUEST" creation policy filter var filteringCatalog = new FilteringCatalog( parentCatalog, p => p.Metadata.ContainsKey(CompositionConstants.PartCreationPolicyMetadataName) && p.Metadata[CompositionConstants.PartCreationPolicyMetadataName].Equals(CreationPolicy.Shared) && p.Metadata.ContainsKey("PerRequest") && p.Metadata["PerRequest"].Equals(true) ); // Create the child container this.requestContainer = new CompositionContainer(filteringCatalog, parentContainer); }
public void ParentChildContainerTest() { var catalog = new AssemblyCatalog(typeof(FilteringCatalogTests).Assembly); var parent = new CompositionContainer(catalog); var filteredCat = new FilteringCatalog(catalog, new HasCreationPolicy(CreationPolicy.NonShared)); var child = new CompositionContainer(filteredCat, parent); var root = child.GetExportedValue<Root>(); var dep1 = root.Dep; var dep2 = dep1.Dep; var dep3 = dep2.Dep; Assert.That(root.Disposed, Is.False); Assert.That(dep1.Disposed, Is.False); Assert.That(dep2.Disposed, Is.False); Assert.That(dep3.Disposed, Is.False); child.Dispose(); Assert.That(root.Disposed, Is.True); // Disposed as it was created by the child container Assert.That(dep1.Disposed, Is.True); // Disposed as it was created by the child container Assert.That(dep2.Disposed, Is.False); Assert.That(dep3.Disposed, Is.False); }