Pavas.Patterns.Context 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pavas.Patterns.Context --version 1.0.1                
NuGet\Install-Package Pavas.Patterns.Context -Version 1.0.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="Pavas.Patterns.Context" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pavas.Patterns.Context --version 1.0.1                
#r "nuget: Pavas.Patterns.Context, 1.0.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.
// Install Pavas.Patterns.Context as a Cake Addin
#addin nuget:?package=Pavas.Patterns.Context&version=1.0.1

// Install Pavas.Patterns.Context as a Cake Tool
#tool nuget:?package=Pavas.Patterns.Context&version=1.0.1                

Context Design Pattern In .NET

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;
    }
}

1. Register the Context in Dependency Injection

In your Startup.cs or wherever you configure your services, register the context using the provided extension method.

public void ConfigureServices(IServiceCollection services)
{
    services.AddContext<MyContext>();
}

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;
        if (context != null)
        {
            // Use the context (e.g., log the UserId and TenantId)
            Console.WriteLine($"UserId: {context.UserId}, TenantId: {context.TenantId}, CorrelationId: {context.TenantId}");
        }
    }
}
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:

  • ContextProvider<TContext>: Manages the current instance of the context for the current execution 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.0.5 87 9/23/2024
1.0.4 193 8/22/2024
1.0.3 121 8/22/2024
1.0.2 131 8/20/2024
1.0.1 127 8/20/2024
1.0.0 121 8/20/2024