Class ScopedCollection<T>
- Namespace
- Kampute.HttpClient.Utilities
- Assembly
- Kampute.HttpClient.dll
Manages items within specific contexts.
public class ScopedCollection<T> : IEnumerable<T>, IEnumerable
Type Parameters
T
- Manages items within specific contexts.
- Inheritance
-
ScopedCollection<T>
- Implements
-
IEnumerable<T>
- Inherited Members
Remarks
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.
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
items
IEnumerable<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
items
is null.
EndScope(Scope)
Ends the specified scope and removes it from the current context.
protected virtual void EndScope(ScopedCollection<T>.Scope scope)
Parameters
scope
ScopedCollection<T>.ScopeThe scope to be removed.
Exceptions
- ArgumentNullException
Thrown if
scope
is null.
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
action
Action<T>The action to perform on each item within the scopes.
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.
Exceptions
- ArgumentNullException
Thrown if the
action
is null.