Class RequestContext
- Namespace
- SimpleMediator
- Assembly
- SimpleMediator.dll
Default implementation of IRequestContext.
public sealed class RequestContext : IRequestContext
- Inheritance
-
RequestContext
- Implements
- Inherited Members
Remarks
Immutable by design - all With* methods return new instances.
Thread-safe for concurrent access.
Properties
CorrelationId
Correlation ID for distributed tracing.
public string CorrelationId { get; init; }
Property Value
Remarks
Always present - auto-generated from Current or Guid if not provided. Use this for linking logs, traces, and metrics across services.
IdempotencyKey
Idempotency key for duplicate detection.
public string? IdempotencyKey { get; init; }
Property Value
Remarks
null if idempotency is not applicable for this request.
Used by idempotency behaviors to prevent duplicate processing of the same logical request.
Typically extracted from HTTP headers (e.g., Idempotency-Key).
Metadata
Custom metadata for extensibility.
public IReadOnlyDictionary<string, object?> Metadata { get; init; }
Property Value
Remarks
Allows behaviors and processors to attach additional context that doesn't fit standard properties. Use WithMetadata(string, object?) to add entries.
TenantId
Tenant ID for multi-tenant applications.
public string? TenantId { get; init; }
Property Value
Remarks
null if the application is not multi-tenant or tenant cannot be determined.
Used for data isolation and tenant-specific logic.
Timestamp
Request timestamp (UTC).
public DateTimeOffset Timestamp { get; init; }
Property Value
Remarks
Captured when the context is created, represents the start of request processing. Useful for time-based logic, audit trails, and latency measurements.
UserId
User ID initiating the request.
public string? UserId { get; init; }
Property Value
Remarks
null if the request is unauthenticated.
Typically extracted from claims principal in ASP.NET Core applications.
Methods
Create()
Creates a new context with auto-generated correlation ID.
public static IRequestContext Create()
Returns
- IRequestContext
New context instance.
Remarks
Correlation ID is extracted from Current if available, otherwise a new GUID is generated.
Timestamp is set to current UTC time.
Create(string)
Creates a new context with specified correlation ID.
public static IRequestContext Create(string correlationId)
Parameters
correlationIdstringCorrelation ID to use.
Returns
- IRequestContext
New context instance.
Remarks
Useful when correlation ID is provided externally (e.g., from HTTP headers).
CreateForTest(string?, string?, string?, string?)
Creates a test context with specified properties.
public static IRequestContext CreateForTest(string? userId = null, string? tenantId = null, string? idempotencyKey = null, string? correlationId = null)
Parameters
userIdstringUser ID (optional).
tenantIdstringTenant ID (optional).
idempotencyKeystringIdempotency key (optional).
correlationIdstringCorrelation ID (optional, auto-generated if not provided).
Returns
- IRequestContext
New context instance.
Remarks
Helper method for unit tests. Provides a fluent way to create contexts with specific values.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.
WithIdempotencyKey(string?)
Creates a new context with updated idempotency key.
public IRequestContext WithIdempotencyKey(string? idempotencyKey)
Parameters
idempotencyKeystringIdempotency key to set.
Returns
- IRequestContext
New context instance with the idempotency key updated.
Remarks
Follows immutable pattern - original context is not modified. Useful in pre-processors that extract idempotency headers.
WithMetadata(string, object?)
Creates a new context with additional metadata.
public IRequestContext WithMetadata(string key, object? value)
Parameters
Returns
- IRequestContext
New context instance with the metadata added.
Remarks
Follows immutable pattern - original context is not modified.
WithTenantId(string?)
Creates a new context with updated tenant ID.
public IRequestContext WithTenantId(string? tenantId)
Parameters
tenantIdstringTenant ID to set.
Returns
- IRequestContext
New context instance with the tenant ID updated.
Remarks
Follows immutable pattern - original context is not modified. Useful in pre-processors that determine tenant from request data or claims.
WithUserId(string?)
Creates a new context with updated user ID.
public IRequestContext WithUserId(string? userId)
Parameters
userIdstringUser ID to set.
Returns
- IRequestContext
New context instance with the user ID updated.
Remarks
Follows immutable pattern - original context is not modified. Useful in pre-processors that extract user identity.