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