1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.Collections.Generic;
\r
6 using System.ComponentModel.Composition.Primitives;
\r
8 using Microsoft.Internal;
\r
9 using System.Collections.ObjectModel;
\r
10 using System.Diagnostics.CodeAnalysis;
\r
12 namespace System.ComponentModel.Composition.Hosting
\r
14 public partial class CompositionBatch
\r
16 private object _lock = new object();
\r
17 private bool _copyNeededForAdd;
\r
18 private bool _copyNeededForRemove;
\r
19 private List<ComposablePart> _partsToAdd;
\r
20 private ReadOnlyCollection<ComposablePart> _readOnlyPartsToAdd;
\r
21 private List<ComposablePart> _partsToRemove;
\r
22 private ReadOnlyCollection<ComposablePart> _readOnlyPartsToRemove;
\r
25 /// Initializes a new instance of the <see cref="CompositionBatch"/> class.
\r
27 public CompositionBatch() :
\r
33 /// Initializes a new instance of the <see cref="CompositionBatch"/> class.
\r
35 /// <param name="partsToAdd">The parts to add.</param>
\r
36 /// <param name="partsToRemove">The parts to remove.</param>
\r
37 public CompositionBatch(IEnumerable<ComposablePart> partsToAdd, IEnumerable<ComposablePart> partsToRemove)
\r
39 this._partsToAdd = new List<ComposablePart>();
\r
40 if (partsToAdd != null)
\r
42 foreach (var part in partsToAdd)
\r
46 throw ExceptionBuilder.CreateContainsNullElement("partsToAdd");
\r
48 this._partsToAdd.Add(part);
\r
51 this._readOnlyPartsToAdd = this._partsToAdd.AsReadOnly();
\r
53 this._partsToRemove = new List<ComposablePart>();
\r
54 if (partsToRemove != null)
\r
56 foreach (var part in partsToRemove)
\r
60 throw ExceptionBuilder.CreateContainsNullElement("partsToRemove");
\r
62 this._partsToRemove.Add(part);
\r
65 this._readOnlyPartsToRemove = this._partsToRemove.AsReadOnly();
\r
69 /// Returns the collection of parts that will be added.
\r
71 /// <value>The parts to be added.</value>
\r
72 public ReadOnlyCollection<ComposablePart> PartsToAdd
\r
78 this._copyNeededForAdd = true;
\r
79 return this._readOnlyPartsToAdd;
\r
85 /// Returns the collection of parts that will be removed.
\r
87 /// <value>The parts to be removed.</value>
\r
88 public ReadOnlyCollection<ComposablePart> PartsToRemove
\r
94 this._copyNeededForRemove = true;
\r
95 return this._readOnlyPartsToRemove;
\r
101 /// Adds the specified part to the <see cref="CompositionBatch"/>.
\r
103 /// <param name="part">
\r
106 /// <exception cref="ArgumentNullException">
\r
107 /// <paramref name="part"/> is <see langword="null"/>.
\r
109 public void AddPart(ComposablePart part)
\r
111 Requires.NotNull(part, "part");
\r
114 if (this._copyNeededForAdd)
\r
116 this._partsToAdd = new List<ComposablePart>(this._partsToAdd);
\r
117 this._readOnlyPartsToAdd = this._partsToAdd.AsReadOnly();
\r
118 this._copyNeededForAdd = false;
\r
120 this._partsToAdd.Add(part);
\r
125 /// Removes the specified part from the <see cref="CompositionBatch"/>.
\r
127 /// <param name="part">
\r
130 /// <exception cref="ArgumentNullException">
\r
131 /// <paramref name="part"/> is <see langword="null"/>.
\r
133 public void RemovePart(ComposablePart part)
\r
135 Requires.NotNull(part, "part");
\r
138 if (this._copyNeededForRemove)
\r
140 this._partsToRemove = new List<ComposablePart>(this._partsToRemove);
\r
141 this._readOnlyPartsToRemove = this._partsToRemove.AsReadOnly();
\r
142 this._copyNeededForRemove = false;
\r
144 this._partsToRemove.Add(part);
\r
149 /// Adds the specified export to the <see cref="CompositionBatch"/>.
\r
151 /// <param name="export">
\r
152 /// The <see cref="Export"/> to add to the <see cref="CompositionBatch"/>.
\r
155 /// A <see cref="ComposablePart"/> that can be used remove the <see cref="Export"/>
\r
156 /// from the <see cref="CompositionBatch"/>.
\r
158 /// <exception cref="ArgumentNullException">
\r
159 /// <paramref name="export"/> is <see langword="null"/>.
\r
163 public ComposablePart AddExport(Export export)
\r
165 Requires.NotNull(export, "export");
\r
167 ComposablePart part = new SingleExportComposablePart(export);
\r
169 this.AddPart(part);
\r