Refactor MemoryCacheExtensions and clean up HistoryController

- Introduced a static readonly field `BaseId` in `MemoryCacheExtensions.cs`.
- Refactored `GetEnumAsDictionary<TEnum>` to use expression-bodied syntax and LINQ for improved readability and efficiency.
- Removed the import statement for `Microsoft.IdentityModel.Tokens` in `HistoryController.cs`, indicating a potential shift in authentication/authorization handling.
This commit is contained in:
Developer 02 2025-05-08 13:52:57 +02:00
parent 7fefc68061
commit 0698b44b68
2 changed files with 7 additions and 12 deletions

View File

@ -1,20 +1,16 @@
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System;
namespace EnvelopeGenerator.Extensions; namespace EnvelopeGenerator.Extensions;
public static class MemoryCacheExtensions public static class MemoryCacheExtensions
{ {
private static readonly Guid BaseId = Guid.NewGuid();
public static IDictionary<string, int> GetEnumAsDictionary<TEnum>(this IMemoryCache memoryCache) public static IDictionary<string, int> GetEnumAsDictionary<TEnum>(this IMemoryCache memoryCache)
where TEnum : Enum where TEnum : Enum
{ => memoryCache.GetOrCreate(BaseId + typeof(TEnum).FullName, _ =>
var dict = new Dictionary<string, int>(); Enum.GetValues(typeof(TEnum))
.Cast<TEnum>()
foreach (TEnum role in Enum.GetValues(typeof(TEnum))) .ToDictionary(e => e.ToString(), e => Convert.ToInt32(e)))
{ ?? throw new InvalidOperationException($"Failed to cache or retrieve enum dictionary for type '{typeof(TEnum).FullName}'.");
dict[role.ToString()] = Convert.ToInt32(role);
}
return dict;
}
} }

View File

@ -5,7 +5,6 @@ using EnvelopeGenerator.Extensions;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.IdentityModel.Tokens;
using static EnvelopeGenerator.Common.Constants; using static EnvelopeGenerator.Common.Constants;