From 0698b44b68b5de2e9e2760e7c3f4151ea03005c0 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Thu, 8 May 2025 13:52:57 +0200 Subject: [PATCH] Refactor MemoryCacheExtensions and clean up HistoryController - Introduced a static readonly field `BaseId` in `MemoryCacheExtensions.cs`. - Refactored `GetEnumAsDictionary` 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. --- .../MemoryCacheExtensions.cs | 18 +++++++----------- .../Controllers/HistoryController.cs | 1 - 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/EnvelopeGenerator.Extensions/MemoryCacheExtensions.cs b/EnvelopeGenerator.Extensions/MemoryCacheExtensions.cs index 31df714d..86ac56bb 100644 --- a/EnvelopeGenerator.Extensions/MemoryCacheExtensions.cs +++ b/EnvelopeGenerator.Extensions/MemoryCacheExtensions.cs @@ -1,20 +1,16 @@ using Microsoft.Extensions.Caching.Memory; -using System; namespace EnvelopeGenerator.Extensions; public static class MemoryCacheExtensions { + private static readonly Guid BaseId = Guid.NewGuid(); + public static IDictionary GetEnumAsDictionary(this IMemoryCache memoryCache) where TEnum : Enum - { - var dict = new Dictionary(); - - foreach (TEnum role in Enum.GetValues(typeof(TEnum))) - { - dict[role.ToString()] = Convert.ToInt32(role); - } - - return dict; - } + => memoryCache.GetOrCreate(BaseId + typeof(TEnum).FullName, _ => + Enum.GetValues(typeof(TEnum)) + .Cast() + .ToDictionary(e => e.ToString(), e => Convert.ToInt32(e))) + ?? throw new InvalidOperationException($"Failed to cache or retrieve enum dictionary for type '{typeof(TEnum).FullName}'."); } diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/HistoryController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/HistoryController.cs index 2adbd4e0..5c55b567 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/HistoryController.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/HistoryController.cs @@ -5,7 +5,6 @@ using EnvelopeGenerator.Extensions; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Caching.Memory; -using Microsoft.IdentityModel.Tokens; using static EnvelopeGenerator.Common.Constants;