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)); } }