Open sandboxFocus

Class IncrementalKeyedCollection<TKey, TValue>

An immutable keyed collection where each class instance does not represent the full collection but a modification of another collection (possibly empty). This class implements the IIncrementalObject interface and can be easily used in the context of an IHierarchicalOptions<T>. The class can represent the AddOrApplyChanges(TValue), Remove(TKey) and Clear<TKey, TValue>() operations.

Inheritance
IncrementalKeyedCollection<TKey, TValue>
Namespace: Metalama.Framework.Options
Assembly: Metalama.Framework.dll
Syntax
public class IncrementalKeyedCollection<TKey, TValue> : IIncrementalObject, IReadOnlyCollection<TValue>, IEnumerable<TValue>, IEnumerable, ICompileTimeSerializable where TKey : notnull where TValue : class, IIncrementalKeyedCollectionItem<TKey>
Type Parameters
Name Description
TKey

Type of keys.

TValue

Type of items, implementing the IIncrementalKeyedCollectionItem<TKey> interface.

Remarks

IncrementalKeyedCollection<TKey, TValue> is designed for use as a property type within hierarchical options classes when you need to work with collections of complex, keyed items. Unlike a regular dictionary or keyed collection, each instance represents a layer of changes rather than the complete collection. When multiple layers are merged using ApplyChanges(IncrementalKeyedCollection<TKey, TValue>, in ApplyChangesContext), the operations from each layer are combined intelligently.

The key feature of this collection is that when an item with the same key exists in both the base collection and the overriding collection, the two items are themselves merged using ApplyChanges(object, in ApplyChangesContext). This allows for fine-grained incremental modifications to complex configuration objects.

For example, if one layer has a rule with key "DefaultPolicy" specifying cache duration of 60 seconds, and another layer provides a rule with the same key specifying a different setting, the two rules are merged, allowing partial updates rather than complete replacement.

This class is immutable. All operations return new instances rather than modifying the current instance.

Constructors

Name Description
IncrementalKeyedCollection(ImmutableDictionary<TKey, Item>, bool)

Properties

Name Description
Count

Gets the number of items in the current collection layer.

Empty

Gets an IncrementalKeyedCollection<TKey, TValue> that represents the absence of any change in the collection.

IsEmpty

Gets a value indicating whether this collection layer is empty.

Methods

Name Description
AddOrApplyChanges(IEnumerable<TValue>)

Creates a IncrementalKeyedCollection<TKey, TValue> that contains all operations already contained in the current object, plus the operation of adding items, or merging them with existing items if the same keys already exist.

AddOrApplyChanges(TValue)

Creates a IncrementalKeyedCollection<TKey, TValue> that contains all operations already contained in the current object, plus the operation of adding an item, or merging it with an existing item if the same key already exists.

AddOrApplyChanges(params TValue[])

Creates a IncrementalKeyedCollection<TKey, TValue> that contains all operations already contained in the current object, plus the operation of adding items, or merging them with existing items if the same keys already exist.

ApplyChanges(IncrementalKeyedCollection<TKey, TValue>, in ApplyChangesContext)

Merges the current collection layer with another collection layer and returns the result.

Create(ImmutableDictionary<TKey, Item>, bool)
GetEnumerator()

Returns an enumerator that iterates through the items in the collection.

Remove(IEnumerable<TKey>)

Creates a IncrementalKeyedCollection<TKey, TValue> that contains all operations already contained in the current object, plus the operation of removing items from the collection.

Remove(TKey)

Creates a IncrementalKeyedCollection<TKey, TValue> that contains all operations already contained in the current object, plus the operation of removing an item from the collection.

Remove(TKey[])

Creates a IncrementalKeyedCollection<TKey, TValue> that contains all operations already contained in the current object, plus the operation of removing items from the collection.

Extension Methods

See Also