NuGet Package Architect.AmbientContexts

Provides the basis for implementing the Ambient Context pattern.

Includes a Clock implementation based on it.

The Ambient Context pattern is an Inversion of Control (IoC) pattern that provides static access to a dependency while controlling the dependency from the outside.
The pattern optimizes code reachability at the cost of reduced transparency, making it suitable for obvious, ubiquitous, rarely-changing dependencies.
For example, an entity's constructor can access Clock.Now, avoiding the need to inject the creation datetime or a dependency, at the cost of hiding the fact that a unit test could control the timestamp from the outside.

An example from .NET is System.Transactions.TransactionScope. Any code (such as the database connector) can access the static Transaction.Current, yet outer code in the current execution flow controls it, through TransactionScopes.

Release notes:

- Manually disposing scopes from a deeper async level (such as a DisposeAsync() method with the async keyword) now properly affects methods up the call stack and no longer breaks scope nesting.
- Improved the protections against race conditions.
- Parent properties are no longer unset on disposal (although implementations should not rely on this detail).
- Performance improvement: A scope now avoids even instantiating its AsyncLocal as long as only its default scope is used, as is common in production for certain scopes.
- Performance improvement: The JIT can now inline more code, since exceptions have been moved into helper methods.

- Introduced non-generic AmbientScope base class.
- Performance improvement: A scope now avoids touching its AsyncLocal as long as only its default scope is used, as is common in production for certain scopes.


Version: 1.1.1-preview-20211023.3
Author(s): TheArchitectDev, Timovzl
Last Update: Saturday, October 23, 2021
.NET Fiddle: Create the first Fiddle
NuGet Url:

Install-Package Architect.AmbientContexts
dotnet add package Architect.AmbientContexts
paket add Architect.AmbientContexts
Architect.AmbientContexts Download (Unzip the "nupkg" after downloading)