What is the Ambient Context Pattern and how does it compare to DI?

Updated Apr 28, 2026

Short answer

Ambient Context provides global access to a specific cross-cutting concern (like a logger or user identity) via a static property.

Deep explanation

It acts as a controlled global variable (e.g., Thread.CurrentPrincipal or TimeProvider.System). While easier to access than passing dependencies through every constructor, it hides dependencies, complicates testing (requires mocking statics), and can cause async context flow issues.

Real-world example

Accessing the current logged-in user's ID deep in a domain model without passing the User object through 5 layers of method calls.

Common mistakes

  • Using Ambient Context for volatile business logic services rather than strictly stable, cross-cutting concerns.

Follow-up questions

  • How do you test code that uses an Ambient Context?

More Dependency Injection interview questions

View all →