From 6943397420b2fd304842db857f710e9d256a4917 Mon Sep 17 00:00:00 2001 From: Ionite Date: Wed, 6 Dec 2023 16:47:57 -0500 Subject: [PATCH] Switch to ConcurrentDictionary for LiteDBExtensions --- .../Extensions/LiteDBExtensions.cs | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/StabilityMatrix.Core/Extensions/LiteDBExtensions.cs b/StabilityMatrix.Core/Extensions/LiteDBExtensions.cs index a40b4213..3377a2e3 100644 --- a/StabilityMatrix.Core/Extensions/LiteDBExtensions.cs +++ b/StabilityMatrix.Core/Extensions/LiteDBExtensions.cs @@ -1,4 +1,5 @@ -using System.Linq.Expressions; +using System.Collections.Concurrent; +using System.Linq.Expressions; using System.Reflection; using LiteDB; using LiteDB.Async; @@ -8,7 +9,8 @@ namespace StabilityMatrix.Core.Extensions; // ReSharper disable once InconsistentNaming public static class LiteDBExtensions { - private static readonly Dictionary Mapper = new(); + private static readonly ConcurrentDictionary Mapper = + new(); public static void Register(Expression?>> exp, string? collection = null) { @@ -16,7 +18,7 @@ public static class LiteDBExtensions if (member == null) throw new ArgumentException("Expecting Member Expression"); BsonMapper.Global.Entity().DbRef(exp, collection); - Mapper.Add(typeof(T), (typeof(TU), member.Name, true)); + Mapper.TryAdd(typeof(T), (typeof(TU), member.Name, true)); } public static void Register(Expression> exp, string? collection = null) @@ -25,12 +27,13 @@ public static class LiteDBExtensions if (member == null) throw new ArgumentException("Expecting Member Expression"); BsonMapper.Global.Entity().DbRef(exp, collection); - Mapper.Add(typeof(T), (typeof(TU), member.Name, false)); + Mapper.TryAdd(typeof(T), (typeof(TU), member.Name, false)); } public static ILiteCollection? IncludeAll(this ILiteCollection col) { - if (!Mapper.ContainsKey(typeof(T))) return null; + if (!Mapper.ContainsKey(typeof(T))) + return null; var stringList = new List(); var key = typeof(T); @@ -45,12 +48,13 @@ public static class LiteDBExtensions flag = false; } - return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression) keySelector)); + return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression)keySelector)); } - + public static ILiteCollectionAsync IncludeAll(this ILiteCollectionAsync col) { - if (!Mapper.ContainsKey(typeof(T))) return col; + if (!Mapper.ContainsKey(typeof(T))) + return col; var stringList = new List(); var key = typeof(T); @@ -65,6 +69,6 @@ public static class LiteDBExtensions flag = false; } - return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression) keySelector)); + return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression)keySelector)); } }