ModularityKit.Context
1.0.0-preview.1
dotnet add package ModularityKit.Context --version 1.0.0-preview.1
NuGet\Install-Package ModularityKit.Context -Version 1.0.0-preview.1
<PackageReference Include="ModularityKit.Context" Version="1.0.0-preview.1" />
<PackageVersion Include="ModularityKit.Context" Version="1.0.0-preview.1" />
<PackageReference Include="ModularityKit.Context" />
paket add ModularityKit.Context --version 1.0.0-preview.1
#r "nuget: ModularityKit.Context, 1.0.0-preview.1"
#:package ModularityKit.Context@1.0.0-preview.1
#addin nuget:?package=ModularityKit.Context&version=1.0.0-preview.1&prerelease
#tool nuget:?package=ModularityKit.Context&version=1.0.0-preview.1&prerelease
Context System
A thread-safe, async-safe context management system with built-in security boundaries for .NET applications.
🚀 Features
- Thread-Safe & Async-Safe - Built on
AsyncLocal<T>for automatic context isolation - Security Boundaries - Defensive copy pattern prevents untrusted code from accessing sensitive data
- Multi-Tenant Ready - Perfect for SaaS applications requiring tenant isolation
- Plugin-Friendly - Safe context exposure to third-party plugins
- Distributed Tracing - Built-in support for correlation IDs and trace context
- High Performance - Minimal overhead (~100ns per operation)
⚡ Quick Start
using ModularityKit.Context.Abstractions;
using ModularityKit.Context.AspNet;
// 1. Define your context
public class MyContext : IContext
{
public string Id { get; }
public DateTimeOffset CreatedAt { get; }
public string UserId { get; }
public string TenantId { get; }
}
// 2. Register in DI
services.AddContext();
// 3. Use in your code
public class OrderService(IContextAccessor<MyContext> context)
{
public void ProcessOrder()
{
var ctx = context.RequireCurrent();
Console.WriteLine($"Processing for user: {ctx.UserId}");
}
}
// 4. Execute with context
var context = new MyContext("ctx-1", "user-123", "tenant-456");
await contextManager.ExecuteInContext(context, async () =>
{
orderService.ProcessOrder();
});
🎯 Why Use Context System?
Without Context System ❌
public class OrderService
{
// Passing context everywhere - painful!
public async Task CreateOrder(string userId, string tenantId, string correlationId)
{
await ValidateOrder(userId, tenantId, correlationId);
await ProcessPayment(userId, tenantId, correlationId);
await SendEmail(userId, tenantId, correlationId);
}
}
With Context System ✅
public class OrderService(IContextAccessor<MyContext> context)
{
// Clean API - context flows automatically
public async Task CreateOrder()
{
await ValidateOrder(); // Context flows automatically
await ProcessPayment();
await SendEmail();
}
}
📚 Documentation
Getting Started
- Installation & Setup - Complete setup guide
- Basic Usage - Learn the fundamentals
- Core Concepts - Understanding the architecture
Guides
- Multi-Tenant Applications - Building SaaS applications
Reference
- API Reference - Complete API documentation
- Best Practices - Tips and recommendations
🏢 Multi-Tenant SaaS
public class DocumentService(IContextAccessor<MyContext> context)
{
public async Task GetDocument(string documentId)
{
var ctx = context.RequireCurrent();
// Automatic tenant filtering
return await _db.Documents
.Where(d => d.TenantId == ctx.TenantId)
.Where(d => d.Id == documentId)
.FirstOrDefaultAsync();
}
}
Automatic tenant isolation using
IContextAccessorensures security and data separation.
🏗️ Architecture
Application Layer
├─ Trusted Code (Full Access)-> IContextAccessor<MyContext>
└─ Untrusted Code (Read-Only) -> IContextAccessor<IReadOnlyContext>
│
▼
Context Infrastructure
├─ ContextStore (AsyncLocal)
├─ ContextAccessor
└─ ContextManager
│
▼
Security Boundary
└─ ReadOnlyContextSnapshot (Defensive Copy)
Learn more about architecture →
✅ Best Practices
DO ✅
- Create context per operation (request/job)
- Use
RequireCurrent()when context is expected - Let
ExecuteInContexthandle cleanup - Treat contexts as immutable
DON'T ❌
- Share context across operations
- Mutate context properties
- Use
Currentwithout null check - Store context in static fields
Read full best practices guide →
📖 API Reference
Core Interfaces
- IContext - Base context interface
- IContextAccessor<TContext> - Access current context
- IContextManager<TContext> - Manage context lifecycle
Extension Methods
- AddContext<TContext>() - Register context infrastructure
- AddReadOnlyContextAccessor<TContext>() - Register read-only accessor
Built with ❤️ for .NET developers
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on ModularityKit.Context:
| Package | Downloads |
|---|---|
|
ModularityKit.Context.AspNet
Context abstraction and propagation for Modularity framework |
|
|
ModularityKit.Context.Autofac
Context abstraction and propagation for Modularity framework |
|
|
ModularityKit.Context.SimpleInjector
Context abstraction and propagation for Modularity framework |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-preview.1 | 298 | 12/19/2025 |