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:
parent
7fefc68061
commit
0698b44b68
@ -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<string, int> GetEnumAsDictionary<TEnum>(this IMemoryCache memoryCache)
|
||||
where TEnum : Enum
|
||||
{
|
||||
var dict = new Dictionary<string, int>();
|
||||
|
||||
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<TEnum>()
|
||||
.ToDictionary(e => e.ToString(), e => Convert.ToInt32(e)))
|
||||
?? throw new InvalidOperationException($"Failed to cache or retrieve enum dictionary for type '{typeof(TEnum).FullName}'.");
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user