Plinth Common Utilities
$ dotnet add package Plinth.CommonA set of generic utilities, extensions, and enhancements for dotnet applications
Extensions for atomic GetOrCreate on Microsoft.Extensions.Caching.Memory memory caches Example:
IMemoryCache memCache = MemoryCacheUtil.Create();
await memCache.GetOrCreateAtomicMaxDurationAsync(
"key", TimeSpan.FromMinutes(2), async () => await LoadValue());
:point_right: MemoryCacheUtil.Create() is a shorthand method for creating a new IMemoryCache.
// values are case insensitive
var set = new CaseInsensitiveHashSet<string>() { "abc", "ABC" };
set.Contains("aBc"); // true
// LINQ extension
var set = items.Where(...).Select(...).ToCaseInsensitiveHashSet();
// keys are case insensitive
var dict = new CaseInsensitiveDictionary<string, int>() { ["abc"] = 5 };
dict["ABC"]; // 5
dict.ContainsKey("aBc"); // true
// LINQ extension
var dict = items.Where(...).Select(...).ToCaseInsensitiveDictionary(v => v.ID);
// default dictionary, easy counting
var dict = new DefaultDictionary<string, int>(0);
dict["a"]++; // dict["a"] now is 1
// default dictionary, easy multi maps
var dict = new DefaultDictionary<string, ISet<int>>(() => new HashSet<int>());
dict["a"].Add(7); // "a" has a set with (7) in it
Handy constants for Content Types
ContentTypes.ApplicationJson => "application/json; charset=utf-8"
Logical exceptions for common API return values. Allows for separation between logic and api projects
A variety of useful extensions to common classes
.GetValueOrDefault().IsNullOrEmpty() and .Many()using (log.LogExecTiming("some code"))
{
... some code ...
log.Debug("action");
}
// logs
[CTX-6540F] : Starting some code #52400089
[CTX-6540F] : action
[CTX-6540F] : some code #52400089 Took: 00.00.00.274472.ToBytes() and .ToBytesAsync().ChangeType<T>() as various .To{Type}OrNull()StaticLogManager.GetLogger() to retrieve a static logger for the current classRequestTraceId and RequestTraceProperties singletons for passing data through the entire call stack (including across HTTP boundaries)LogTrace => TraceLogDebug => DebugLogInformation => InfoLogWarning => WarnLogError => ErrorLogCritical => Critical or FatalAn enhanced version of Omu.ValueInjecter for easy object to object mapping Allows for deeply nested Field to Field mapping (including Lists, Sets, and Dictionaries)
A set of static utilities
Convert.ToHexStringLower() and Convert.FromHexString() instead. HexUtil methods still function on .NET 8.0 and earlier.FindExecutableHome() which can find your project root at runtimeJoin() method similar to Path.Combine()