Pavas.Patterns.Context
1.0.5
dotnet add package Pavas.Patterns.Context --version 1.0.5
NuGet\Install-Package Pavas.Patterns.Context -Version 1.0.5
<PackageReference Include="Pavas.Patterns.Context" Version="1.0.5" />
paket add Pavas.Patterns.Context --version 1.0.5
#r "nuget: Pavas.Patterns.Context, 1.0.5"
// Install Pavas.Patterns.Context as a Cake Addin #addin nuget:?package=Pavas.Patterns.Context&version=1.0.5 // Install Pavas.Patterns.Context as a Cake Tool #tool nuget:?package=Pavas.Patterns.Context&version=1.0.5
Pavas.Patterns.Context
Pavas.Patterns.Context is a .NET library for managing context constructors and accessors (providers). It provides a flexible and extensible way to create, manage, and access contexts in your applications. This library is designed with dependency injection in mind, making it easy to integrate into existing projects.
Installation
To install this library, add the following NuGet package to your project:
dotnet add package Pavas.Patterns.Context
Usage
1. Define Context Class
Create a context class that will be managed by the context provider and factory. This class should contain any information relevant to the context you want to manage.
public class MyContext
{
public string UserId { get; set; }
public string TenantId { get; set; }
public string CorrelationId { get; set; }
public MyContext(string userId, string tenantId, string correlationId)
{
UserId = userId;
TenantId = tenantId;
CorrelationId = correlationId;
}
}
2. Register the Context in Dependency Injection
In your Startup.cs
or wherever you configure your services, register the context using the provided extension method.
Use the ServiceLifetime
enum to assign the injection type.
For Scoped Context:
public void ConfigureServices(IServiceCollection services)
{
services.AddScopedContext<MyContext>();
}
For Transient Context:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransientContext<MyContext>();
}
For Singleton Context:
public void ConfigureServices(IServiceCollection services)
{
var myContext = new MyContext("user-123", "tenant-456", "correlation-789");
services.AddSingletonContext(myContext);
}
Or with an initializer:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingletonContext(provider =>
{
return new MyContext("user-123", "tenant-456", "correlation-789");
});
}
3. Create and Use Contexts
Use the IContextFactory<TContext>
to create (construct) and destroy (destruct) contexts within your application.
The IContextProvider<TContext>
will allow you to access the current context.
Constructing a Context
public class SomeService
{
private readonly IContextFactory<MyContext> _contextFactory;
public SomeService(IContextFactory<MyContext> contextFactory)
{
_contextFactory = contextFactory;
}
public void CreateContext()
{
var context = new MyContext("user-123", "tenant-456", "correlation-456");
_contextFactory.Construct(context);
}
}
Accessing the Context
public class AnotherService
{
private readonly IContextProvider<MyContext> _contextProvider;
public AnotherService(IContextProvider<MyContext> contextProvider)
{
_contextProvider = contextProvider;
}
public void DoSomethingWithContext()
{
var context = _contextProvider.Context;
Console.WriteLine($"UserId: {context.UserId}, TenantId: {context.TenantId}, CorrelationId: {context.CorrelationId}");
}
}
Destroying a Context
public class CleanupService
{
private readonly IContextFactory<MyContext> _contextFactory;
public CleanupService(IContextFactory<MyContext> contextFactory)
{
_contextFactory = contextFactory;
}
public void Cleanup()
{
_contextFactory.Destruct();
}
}
Project Structure
The library is organized into several key components:
AsyncLocalContextProvider<TContext>
: Manages the current instance of the context for the current async execution context.GlobalContextProvider<TContext>
: Manages a global or singleton instance of the context.ContextFactory<TContext>
: Provides methods to create (construct) and destroy (destruct) contexts.Extensions
: Contains methods for registering the context provider and factory with the dependency injection container.
Contributing
Contributions to this library are welcome. Feel free to open issues or submit pull requests.
License
This project is licensed under the MIT License.
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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on Pavas.Patterns.Context:
Package | Downloads |
---|---|
Pavas.Runtime.TraceContext
Implements a trace context for tracking request flow and debugging information in distributed applications. |
|
Pavas.Runtime.IdentityContext
Implements an identity context for managing user authentication and authorization information in ASP.NET Core applications. |
|
Pavas.Runtime.MemoryContext
Implements memory context for caching and data persistence in applications, allowing for efficient data retrieval and storage. |
|
Pavas.Runtime.ApplicationContext
Provides centralized configuration and context management for applications, including application name, version, build mode, and environment. |
|
Pavas.Runtime.TenantContext
Enables multi-tenancy support in applications by managing tenant-specific data and configurations. |
GitHub repositories
This package is not used by any popular GitHub repositories.