ScopedCollection<T> Class
- Namespace
- Kampute.HttpClient.Utilities
- Assembly
- Kampute.HttpClient.dll
Definition
Manages items within specific contexts.
public class ScopedCollection<T> : IEnumerable<T>- Inheritance
- object
- ScopedCollection<T>
- Implements
- IEnumerable<T>
Type Parameters
T- The type of items managed within the scopes.
Remarks
Constructors
ScopedCollection<T>()
Initializes a new instance of ScopedCollection<T> class.
public ScopedCollection<T>()Properties
HasActiveScope
Gets a value indicating whether the current context has an active scope.
public bool HasActiveScope { get; }Property Value
Methods
BeginScope(IEnumerable<T>)
Initiates a new scope within the current context, incorporating the specified items.
public virtual ScopedCollection<T>.Scope BeginScope(IEnumerable<T> items)Parameters
itemsIEnumerable<T>- The items to include in the new scope.
Returns
- ScopedCollection<T>.Scope
- A new instance of the ScopedCollection<T>.Scope class, containing the specified items.
Exceptions
- ArgumentNullException
- Thrown if
itemsisnull.
EndScope(ScopedCollection<T>.Scope)
Ends the specified scope and removes it from the current context.
protected virtual void EndScope(ScopedCollection<T>.Scope scope)Parameters
scopeScopedCollection<T>.Scope- The scope to be removed.
Exceptions
- ArgumentNullException
- Thrown if
scopeisnull.
GetEnumerator()
Returns an enumerator that iterates through the collection of items in the current context.
public virtual IEnumerator<T> GetEnumerator()Returns
- IEnumerator<T>
- An enumerator that can be used to iterate through the collection.
Traverse(Action<T>)
Traverses items in each scope from the innermost to the outermost, applying an action to each item.
public virtual void Traverse(Action<T> action)Parameters
actionAction<T>- The action to perform on each item within the scopes.
Exceptions
- ArgumentNullException
- Thrown if the
actionisnull.
Remarks
Unlike the standard enumeration, which traverses items from outermost to innermost scopes, this method traverses the scopes starting from the current active scope and moving outward to the parent scopes. This order ensures that actions are performed on items starting from the most specific (innermost) to the most general (outermost) context.
Explicit Interface Implementations
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through the collection.
IEnumerator IEnumerable.GetEnumerator()Returns
- IEnumerator
- An enumerator that can be used to iterate through the collection.

This class facilitates the management of contextual items, which are elements associated with distinct operational contexts, such as HTTP requests, database transactions, or other scenarios requiring contextual data preservation.
When enumerating the collection, items are presented from the outermost to the innermost scope, ensuring that items in outer scopes are encountered before those in nested scopes. This ordering reflects the hierarchical relationship, where items defined in outer scopes may be overridden by those in inner scopes.
This class is thread-safe and can be utilized reliably in concurrent and asynchronous operations, ensuring that contextual items remain accessible and intact across the lifespan of a context.