ModularityKit.Context 1.0.0-preview.1

This is a prerelease version of ModularityKit.Context.
dotnet add package ModularityKit.Context --version 1.0.0-preview.1
                    
NuGet\Install-Package ModularityKit.Context -Version 1.0.0-preview.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ModularityKit.Context" Version="1.0.0-preview.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ModularityKit.Context" Version="1.0.0-preview.1" />
                    
Directory.Packages.props
<PackageReference Include="ModularityKit.Context" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ModularityKit.Context --version 1.0.0-preview.1
                    
#r "nuget: ModularityKit.Context, 1.0.0-preview.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ModularityKit.Context@1.0.0-preview.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ModularityKit.Context&version=1.0.0-preview.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ModularityKit.Context&version=1.0.0-preview.1&prerelease
                    
Install as a Cake Tool

Context System

License .NET

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

Guides

Reference


🏢 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 IContextAccessor ensures security and data separation.

See full example →


🏗️ 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 ExecuteInContext handle cleanup
  • Treat contexts as immutable

DON'T ❌

  • Share context across operations
  • Mutate context properties
  • Use Current without null check
  • Store context in static fields

Read full best practices guide →


📖 API Reference

Core Interfaces

Extension Methods

View complete API reference →


Built with ❤️ for .NET developers

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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