PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/System.Composition.TypedParts/src/System/Composition/Hosting/ContainerConfiguration.cs

https://gitlab.com/0072016/0072016-corefx-
C# | 221 lines | 103 code | 24 blank | 94 comment | 12 complexity | f02e8aeee020d442a463fb0a4a62c642 MD5 | raw file
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Composition.Convention;
  6. using System.Composition.Debugging;
  7. using System.Composition.Hosting.Core;
  8. using System.Composition.TypedParts;
  9. using System.Composition.TypedParts.Util;
  10. using System.Diagnostics;
  11. using System.Linq;
  12. using System.Reflection;
  13. namespace System.Composition.Hosting
  14. {
  15. /// <summary>
  16. /// Configures and constructs a lightweight container.
  17. /// </summary>
  18. [DebuggerTypeProxy(typeof(ContainerConfigurationDebuggerProxy))]
  19. public class ContainerConfiguration
  20. {
  21. private AttributedModelProvider _defaultAttributeContext;
  22. private readonly IList<ExportDescriptorProvider> _addedSources = new List<ExportDescriptorProvider>();
  23. private readonly IList<Tuple<IEnumerable<Type>, AttributedModelProvider>> _types = new List<Tuple<IEnumerable<Type>, AttributedModelProvider>>();
  24. /// <summary>
  25. /// Create the container. The value returned from this method provides
  26. /// the exports in the container, as well as a means to dispose the container.
  27. /// </summary>
  28. /// <returns>The container.</returns>
  29. public CompositionHost CreateContainer()
  30. {
  31. var providers = _addedSources.ToList();
  32. foreach (var typeSet in _types)
  33. {
  34. var ac = typeSet.Item2 ?? _defaultAttributeContext ?? new DirectAttributeContext();
  35. providers.Add(new TypedPartExportDescriptorProvider(typeSet.Item1, ac));
  36. }
  37. return CompositionHost.CreateCompositionHost(providers.ToArray());
  38. }
  39. /// <summary>
  40. /// Add an export descriptor provider to the container.
  41. /// </summary>
  42. /// <param name="exportDescriptorProvider">An export descriptor provider.</param>
  43. /// <returns>A configuration object allowing configuration to continue.</returns>
  44. public ContainerConfiguration WithProvider(ExportDescriptorProvider exportDescriptorProvider)
  45. {
  46. if (exportDescriptorProvider == null) throw new ArgumentNullException(nameof(exportDescriptorProvider));
  47. _addedSources.Add(exportDescriptorProvider);
  48. return this;
  49. }
  50. /// <summary>
  51. /// Add conventions defined using a <see cref="AttributedModelProvider"/> to the container.
  52. /// These will be used as the default conventions; types and assemblies added with a
  53. /// specific convention will use their own.
  54. /// </summary>
  55. /// <param name="conventions"></param>
  56. /// <returns>A configuration object allowing configuration to continue.</returns>
  57. public ContainerConfiguration WithDefaultConventions(AttributedModelProvider conventions)
  58. {
  59. if (conventions == null) throw new ArgumentNullException(nameof(conventions));
  60. if (_defaultAttributeContext != null)
  61. throw new InvalidOperationException(System.Composition.Properties.Resources.ContainerConfiguration_DefaultConventionSet);
  62. _defaultAttributeContext = conventions;
  63. return this;
  64. }
  65. /// <summary>
  66. /// Add a part type to the container. If the part type does not have any exports it
  67. /// will be ignored.
  68. /// </summary>
  69. /// <param name="partType">The part type.</param>
  70. /// <returns>A configuration object allowing configuration to continue.</returns>
  71. public ContainerConfiguration WithPart(Type partType)
  72. {
  73. return WithPart(partType, null);
  74. }
  75. /// <summary>
  76. /// Add a part type to the container. If the part type does not have any exports it
  77. /// will be ignored.
  78. /// </summary>
  79. /// <param name="partType">The part type.</param>
  80. /// <param name="conventions">Conventions represented by a <see cref="AttributedModelProvider"/>, or null.</param>
  81. /// <returns>A configuration object allowing configuration to continue.</returns>
  82. public ContainerConfiguration WithPart(Type partType, AttributedModelProvider conventions)
  83. {
  84. if (partType == null) throw new ArgumentNullException(nameof(partType));
  85. return WithParts(new[] { partType }, conventions);
  86. }
  87. /// <summary>
  88. /// Add a part type to the container. If the part type does not have any exports it
  89. /// will be ignored.
  90. /// </summary>
  91. /// <typeparam name="TPart">The part type.</typeparam>
  92. /// <returns>A configuration object allowing configuration to continue.</returns>
  93. public ContainerConfiguration WithPart<TPart>()
  94. {
  95. return WithPart<TPart>(null);
  96. }
  97. /// <summary>
  98. /// Add a part type to the container. If the part type does not have any exports it
  99. /// will be ignored.
  100. /// </summary>
  101. /// <typeparam name="TPart">The part type.</typeparam>
  102. /// <param name="conventions">Conventions represented by a <see cref="AttributedModelProvider"/>, or null.</param>
  103. /// <returns>A configuration object allowing configuration to continue.</returns>
  104. public ContainerConfiguration WithPart<TPart>(AttributedModelProvider conventions)
  105. {
  106. return WithPart(typeof(TPart), conventions);
  107. }
  108. /// <summary>
  109. /// Add part types to the container. If a part type does not have any exports it
  110. /// will be ignored.
  111. /// </summary>
  112. /// <param name="partTypes">The part types.</param>
  113. /// <returns>A configuration object allowing configuration to continue.</returns>
  114. public ContainerConfiguration WithParts(params Type[] partTypes)
  115. {
  116. return WithParts((IEnumerable<Type>)partTypes);
  117. }
  118. /// <summary>
  119. /// Add part types to the container. If a part type does not have any exports it
  120. /// will be ignored.
  121. /// </summary>
  122. /// <param name="partTypes">The part types.</param>
  123. /// <returns>A configuration object allowing configuration to continue.</returns>
  124. public ContainerConfiguration WithParts(IEnumerable<Type> partTypes)
  125. {
  126. return WithParts(partTypes, null);
  127. }
  128. /// <summary>
  129. /// Add part types to the container. If a part type does not have any exports it
  130. /// will be ignored.
  131. /// </summary>
  132. /// <param name="partTypes">The part types.</param>
  133. /// <param name="conventions">Conventions represented by a <see cref="AttributedModelProvider"/>, or null.</param>
  134. /// <returns>A configuration object allowing configuration to continue.</returns>
  135. public ContainerConfiguration WithParts(IEnumerable<Type> partTypes, AttributedModelProvider conventions)
  136. {
  137. if (partTypes == null) throw new ArgumentNullException(nameof(partTypes));
  138. _types.Add(Tuple.Create(partTypes, conventions));
  139. return this;
  140. }
  141. /// <summary>
  142. /// Add part types from an assembly to the container. If a part type does not have any exports it
  143. /// will be ignored.
  144. /// </summary>
  145. /// <param name="assembly">The assembly from which to add part types.</param>
  146. /// <returns>A configuration object allowing configuration to continue.</returns>
  147. public ContainerConfiguration WithAssembly(Assembly assembly)
  148. {
  149. return WithAssembly(assembly, null);
  150. }
  151. /// <summary>
  152. /// Add part types from an assembly to the container. If a part type does not have any exports it
  153. /// will be ignored.
  154. /// </summary>
  155. /// <param name="assembly">The assembly from which to add part types.</param>
  156. /// <param name="conventions">Conventions represented by a <see cref="AttributedModelProvider"/>, or null.</param>
  157. /// <returns>A configuration object allowing configuration to continue.</returns>
  158. public ContainerConfiguration WithAssembly(Assembly assembly, AttributedModelProvider conventions)
  159. {
  160. return WithAssemblies(new[] { assembly }, conventions);
  161. }
  162. /// <summary>
  163. /// Add part types from a list of assemblies to the container. If a part type does not have any exports it
  164. /// will be ignored.
  165. /// </summary>
  166. /// <param name="assemblies">Assemblies containing part types.</param>
  167. /// <returns>A configuration object allowing configuration to continue.</returns>
  168. public ContainerConfiguration WithAssemblies(IEnumerable<Assembly> assemblies)
  169. {
  170. return WithAssemblies(assemblies, null);
  171. }
  172. /// <summary>
  173. /// Add part types from a list of assemblies to the container. If a part type does not have any exports it
  174. /// will be ignored.
  175. /// </summary>
  176. /// <param name="assemblies">Assemblies containing part types.</param>
  177. /// <param name="conventions">Conventions represented by a <see cref="AttributedModelProvider"/>, or null.</param>
  178. /// <returns>A configuration object allowing configuration to continue.</returns>
  179. public ContainerConfiguration WithAssemblies(IEnumerable<Assembly> assemblies, AttributedModelProvider conventions)
  180. {
  181. if (assemblies == null) throw new ArgumentNullException(nameof(assemblies));
  182. return WithParts(assemblies.SelectMany(a => a.DefinedTypes.Select(dt => dt.AsType())), conventions);
  183. }
  184. internal ExportDescriptorProvider[] DebugGetAddedExportDescriptorProviders()
  185. {
  186. return _addedSources.ToArray();
  187. }
  188. internal Tuple<IEnumerable<Type>, AttributedModelProvider>[] DebugGetRegisteredTypes()
  189. {
  190. return _types.ToArray();
  191. }
  192. internal AttributedModelProvider DebugGetDefaultAttributeContext()
  193. {
  194. return _defaultAttributeContext;
  195. }
  196. }
  197. }