public void ImportIntoUntypedExportTest() { var container = ContainerFactory.Create(); CompositionBatch batch = new CompositionBatch(); batch.AddExportedValue("untyped", 42); var u = new UntypedExportImporter(); var rb = AttributedModelServices.CreatePart(u); batch.AddPart(rb); container.Compose(batch); Assert.AreEqual(42, u.Export.Value); var us = new UntypedExportsImporter(); batch = new CompositionBatch(); batch.AddExportedValue("untyped", 19); batch.RemovePart(rb); batch.AddPart(us); container.Compose(batch); Assert.IsNotNull(us.Exports, "Should have an enumeration"); Assert.AreEqual(2, us.Exports.Count(), "Should have 2 values"); }
public void DerivedMetadataAttributeAttribute_ShouldSupplyMetadata() { ComposablePart part = AttributedModelServices.CreatePart(new ExportWithDerivedMetadataAttribute()); ExportDefinition export = part.ExportDefinitions.Single(); Assert.AreEqual("BaseValue", export.Metadata["BaseKey"]); Assert.AreEqual("DerivedValue", export.Metadata["DerivedKey"]); }
/// <summary>Fills the imports of the specified attributed part.</summary> /// <param name="attributedPart">The attributed part to fill the imports of.</param> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="attributedPart" /> is null.</exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="attributedPart" /> contains exports.</exception> /// <exception cref="T:System.ComponentModel.Composition.ChangeRejectedException">One or more of the imports of <paramref name="attributedPart" /> could not be satisfied.</exception> /// <exception cref="T:System.ComponentModel.Composition.CompositionException">One or more of the imports of <paramref name="attributedPart" /> caused a composition error.</exception> public static void SatisfyImports(object attributedPart) { // Note any object can be passed here then we will look up thru reflection what's inside if (attributedPart == null) { throw new ArgumentNullException(nameof(attributedPart)); } SatisfyImports(AttributedModelServices.CreatePart(attributedPart)); }
/// <summary> /// Will satisfy the imports on a object instance based on a <see cref="CompositionContainer"/> /// registered with the <see cref="CompositionHost"/>. By default if no <see cref="CompositionContainer"/> /// is registered the first time this is called it will be initialized to a catalog /// that contains all the assemblies loaded by the initial application XAP. /// </summary> /// <param name="attributedPart"> /// Object instance that contains <see cref="ImportAttribute"/>s that need to be satisfied. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="instance"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="instance"/> contains <see cref="ExportAttribute"/>s applied on its type. /// </exception> /// <exception cref="ChangeRejectedException"> /// One or more of the imports on the object instance could not be satisfied. /// </exception> /// <exception cref="CompositionException"> /// One or more of the imports on the object instance caused an error while composing. /// </exception> public static void SatisfyImports(object attributedPart) { if (attributedPart == null) { throw new ArgumentNullException("attributedPart"); } ComposablePart part = AttributedModelServices.CreatePart(attributedPart); CompositionInitializer.SatisfyImports(part); }
public static void ComposeParts(this CompositionContainer container, params object[] attributedParts) { Requires.NotNull(container, nameof(container)); Requires.NotNullOrNullElements(attributedParts, nameof(attributedParts)); CompositionBatch batch = new CompositionBatch( attributedParts.Select(attributedPart => AttributedModelServices.CreatePart(attributedPart)).ToArray(), Enumerable.Empty <ComposablePart>()); container.Compose(batch); }
/// <summary> /// Satisfies the imports of the specified attributed object exactly once and they will not /// ever be recomposed. /// </summary> /// <param name="part"> /// The attributed object to set the imports. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="attributedPart"/> is <see langword="null"/>. /// </exception> /// <exception cref="CompositionException"> /// An error occurred during composition. <see cref="CompositionException.Errors"/> will /// contain a collection of errors that occurred. /// </exception> /// <exception cref="ObjectDisposedException"> /// The <see cref="ICompositionService"/> has been disposed of. /// </exception> public static ComposablePart SatisfyImportsOnce(this ICompositionService compositionService, object attributedPart) { Requires.NotNull(compositionService, "compositionService"); Requires.NotNull(attributedPart, "attributedPart"); ComposablePart part = AttributedModelServices.CreatePart(attributedPart); compositionService.SatisfyImportsOnce(part); return(part); }
public static ComposablePart AddPart(this CompositionBatch batch, object attributedPart) { Requires.NotNull(batch, "batch"); Requires.NotNull(attributedPart, "attributedPart"); ComposablePart part = AttributedModelServices.CreatePart(attributedPart); batch.AddPart(part); return(part); }
public void InvalidDuplicateMetadataOnType_ShouldThrow() { var part = AttributedModelServices.CreatePart(new ClassWithInvalidDuplicateMetadataOnType()); var export = part.ExportDefinitions.First(); var ex = ExceptionAssert.Throws <InvalidOperationException>(RetryMode.DoNotRetry, () => { var metadata = export.Metadata; }); Assert.True(ex.Message.Contains("DuplicateMetadataName")); }
public void InvalidAttributType_VersionPropertyType_ShouldThrow() { ComposablePart part = AttributedModelServices.CreatePart(new ClassWithInvalidVersionPropertyAttributeType()); ExportDefinition export = part.ExportDefinitions.First(); // Should throw InvalidOperationException during discovery because // the person class is an invalid metadata type ExceptionAssert.Throws <InvalidOperationException>(RetryMode.DoNotRetry, () => { var metadata = export.Metadata; }); }
public void InvalidMetadataAttributeTest() { ComposablePart part = AttributedModelServices.CreatePart(new BasicTestComponentWithInvalidMetadata()); ExportDefinition export = part.ExportDefinitions.First(); var ex = ExceptionAssert.Throws <InvalidOperationException>(RetryMode.DoNotRetry, () => { var metadata = export.Metadata; }); Assert.IsTrue(ex.Message.Contains("Bar")); }
/// <summary> /// Satisfies the imports of the specified attributed object exactly once and they will not /// ever be recomposed. /// </summary> /// <param name="compositionService">The composition service to use.</param> /// <param name="attributedPart"> /// The attributed object to set the imports. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="compositionService"/> or <paramref name="attributedPart"/> is <see langword="null"/>. /// </exception> /// <exception cref="CompositionException"> /// An error occurred during composition. <see cref="CompositionException.Errors"/> will /// contain a collection of errors that occurred. /// </exception> /// <exception cref="ObjectDisposedException"> /// The <see cref="ICompositionService"/> has been disposed of. /// </exception> public static ComposablePart SatisfyImportsOnce(this ICompositionService compositionService, object attributedPart) { Requires.NotNull(compositionService, nameof(compositionService)); Requires.NotNull(attributedPart, nameof(attributedPart)); ComposablePart part = AttributedModelServices.CreatePart(attributedPart); compositionService.SatisfyImportsOnce(part); Debug.Assert(part != null); return(part); }
/// <summary> /// Satisfies the imports of the specified attributed object exactly once and they will not /// ever be recomposed. /// </summary> /// <param name="part"> /// The attributed object to set the imports. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="compositionService"/> or <paramref name="attributedPart"/> is <see langword="null"/>. /// </exception> /// <exception cref="CompositionException"> /// An error occurred during composition. <see cref="CompositionException.Errors"/> will /// contain a collection of errors that occurred. /// </exception> /// <exception cref="ObjectDisposedException"> /// The <see cref="ICompositionService"/> has been disposed of. /// </exception> public static ComposablePart SatisfyImportsOnce(this ICompositionService compositionService, object attributedPart) { Requires.NotNull(compositionService, nameof(compositionService)); Requires.NotNull(attributedPart, nameof(attributedPart)); Contract.Ensures(Contract.Result <ComposablePart>() != null); ComposablePart part = AttributedModelServices.CreatePart(attributedPart); compositionService.SatisfyImportsOnce(part); return(part); }
public static ComposablePart AddPart(this CompositionBatch batch, object attributedPart) { Requires.NotNull(batch, nameof(batch)); Requires.NotNull(attributedPart, nameof(attributedPart)); ComposablePart part = AttributedModelServices.CreatePart(attributedPart); batch.AddPart(part); Debug.Assert(part != null); return(part); }
public static ComposablePart AddPart(this CompositionBatch batch, object attributedPart) { Requires.NotNull(batch, nameof(batch)); Requires.NotNull(attributedPart, nameof(attributedPart)); Contract.Ensures(Contract.Result <ComposablePart>() != null); ComposablePart part = AttributedModelServices.CreatePart(attributedPart); batch.AddPart(part); return(part); }
public void InvalidMetadata_UseOfReservedName_ShouldThrow() { var part = AttributedModelServices.CreatePart(new ClassWithReservedMetadataValue()); var export = part.ExportDefinitions.First(); var ex = ExceptionAssert.Throws <InvalidOperationException>(RetryMode.DoNotRetry, () => { var metadata = export.Metadata; }); Assert.True(ex.Message.Contains(CompositionConstants.PartCreationPolicyMetadataName)); }
/// <summary> /// Satisfies the imports of the specified attributed object exactly once and they will not /// ever be recomposed. /// </summary> /// <param name="part"> /// The attributed object to set the imports. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="compositionService"/> or <paramref name="attributedPart"/> or <paramref name="reflectionContext"/> is <see langword="null"/>. /// </exception> /// <exception cref="CompositionException"> /// An error occurred during composition. <see cref="CompositionException.Errors"/> will /// contain a collection of errors that occurred. /// </exception> /// <exception cref="ObjectDisposedException"> /// The <see cref="ICompositionService"/> has been disposed of. /// </exception> public static ComposablePart SatisfyImportsOnce(this ICompositionService compositionService, object attributedPart, ReflectionContext reflectionContext) { Requires.NotNull(compositionService, "compositionService"); Requires.NotNull(attributedPart, "attributedPart"); Requires.NotNull(reflectionContext, "reflectionContext"); Contract.Ensures(Contract.Result <ComposablePart>() != null); ComposablePart part = AttributedModelServices.CreatePart(attributedPart, reflectionContext); compositionService.SatisfyImportsOnce(part); return(part); }
public void StronglyTypedStructureTestWithTransparentViews() { var container = ContainerFactory.Create(); CompositionBatch batch = new CompositionBatch(); batch.AddPart(AttributedModelServices.CreatePart(new BasicTestComponent())); container.Compose(batch); var export = container.GetExport <BasicTestComponent, ITrans_StronglyTypedStructure>(); Assert.IsNotNull(export.Metadata, "It should have metadata"); Assert.AreEqual("One", export.Metadata.String1, "Property should copy straight across"); Assert.AreEqual("Two", export.Metadata.String2, "Property should copy straight across"); Assert.IsTrue(export.Metadata.Numbers.Contains(1), "Should have 1 in the list"); Assert.IsTrue(export.Metadata.Numbers.Contains(2), "Should have 2 in the list"); Assert.IsTrue(export.Metadata.Numbers.Contains(3), "Should have 3 in the list"); Assert.AreEqual(3, export.Metadata.Numbers.Length, "Should be three numbers total"); }
public void StronglyTypedStructureTestWithTransparentViews() { var container = ContainerFactory.Create(); CompositionBatch batch = new CompositionBatch(); batch.AddPart(AttributedModelServices.CreatePart(new BasicTestComponent())); container.Compose(batch); var export = container.GetExport <BasicTestComponent, ITrans_StronglyTypedStructure>(); Assert.NotNull(export.Metadata); Assert.Equal("One", export.Metadata.String1); Assert.Equal("Two", export.Metadata.String2); Assert.True(export.Metadata.Numbers.Contains(1), "Should have 1 in the list"); Assert.True(export.Metadata.Numbers.Contains(2), "Should have 2 in the list"); Assert.True(export.Metadata.Numbers.Contains(3), "Should have 3 in the list"); Assert.Equal(3, export.Metadata.Numbers.Length); }
public void UntypedStructureTest() { var container = ContainerFactory.Create(); CompositionBatch batch = new CompositionBatch(); batch.AddPart(AttributedModelServices.CreatePart(new BasicTestComponent())); container.Compose(batch); var export = container.GetExport <BasicTestComponent, IDictionary <string, object> >(); Assert.NotNull(export.Metadata); Assert.Equal("One", export.Metadata["String1"]); Assert.Equal("Two", export.Metadata["String2"]); var e = export.Metadata["Numbers"] as IList <int>; Assert.NotNull(e); Assert.True(e.Contains(1), "Should have 1 in the list"); Assert.True(e.Contains(2), "Should have 2 in the list"); Assert.True(e.Contains(3), "Should have 3 in the list"); Assert.Equal(3, e.Count); }
public void UntypedStructureTest() { var container = ContainerFactory.Create(); CompositionBatch batch = new CompositionBatch(); batch.AddPart(AttributedModelServices.CreatePart(new BasicTestComponent())); container.Compose(batch); var export = container.GetExport <BasicTestComponent, IDictionary <string, object> >(); Assert.IsNotNull(export.Metadata, "It should have metadata"); Assert.AreEqual("One", export.Metadata["String1"], "Property attribute should copy straight across"); Assert.AreEqual("Two", export.Metadata["String2"], "Property attribute should copy straight across"); var e = export.Metadata["Numbers"] as IList <int>; Assert.IsNotNull(e, "Should get a collection of numbers"); Assert.IsTrue(e.Contains(1), "Should have 1 in the list"); Assert.IsTrue(e.Contains(2), "Should have 2 in the list"); Assert.IsTrue(e.Contains(3), "Should have 3 in the list"); Assert.AreEqual(3, e.Count, "Should be three numbers total"); }