Files
cleanuperr/code/Infrastructure/Verticals/Context/ContextProvider.cs
T
2025-02-16 03:17:54 +02:00

25 lines
739 B
C#

using System.Collections.Immutable;
namespace Infrastructure.Verticals.Context;
public static class ContextProvider
{
private static readonly AsyncLocal<ImmutableDictionary<string, object>> _asyncLocalDict = new();
public static void Set(string key, object value)
{
ImmutableDictionary<string, object> currentDict = _asyncLocalDict.Value ?? ImmutableDictionary<string, object>.Empty;
_asyncLocalDict.Value = currentDict.SetItem(key, value);
}
public static object? Get(string key)
{
return _asyncLocalDict.Value?.TryGetValue(key, out object? value) is true ? value : null;
}
public static T? Get<T>(string key) where T : class
{
return Get(key) as T;
}
}