using System.Collections.Immutable; namespace Cleanuparr.Infrastructure.Features.Context; public static class ContextProvider { private static readonly AsyncLocal> _asyncLocalDict = new(); public static void Set(string key, object value) { ImmutableDictionary currentDict = _asyncLocalDict.Value ?? ImmutableDictionary.Empty; _asyncLocalDict.Value = currentDict.SetItem(key, value); } public static void Set(T value) where T : class { string key = typeof(T).Name ?? throw new Exception("Type name is null"); Set(key, value); } public static object? Get(string key) { return _asyncLocalDict.Value?.TryGetValue(key, out object? value) is true ? value : null; } public static T Get(string key) where T : class { return Get(key) as T ?? throw new Exception($"failed to get \"{key}\" from context"); } public static T Get() where T : class { string key = typeof(T).Name ?? throw new Exception("Type name is null"); return Get(key); } }