Browse Source

优化可空字典类型

懒得勤快 1 year ago
parent
commit
4b33079486

+ 6 - 6
Masuit.Tools.Abstractions/Extensions/BaseType/IDictionaryExtensions.cs

@@ -1110,10 +1110,10 @@ public static class IDictionaryExtensions
     /// <typeparam name="TKey"></typeparam>
     /// <param name="source"></param>
     /// <param name="keySelector">键选择器</param>
-    public static Dictionary<TKey, List<TSource>> ToLookupX<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
+    public static NullableDictionary<TKey, List<TSource>> ToLookupX<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
     {
         var items = source as IList<TSource> ?? source.ToList();
-        var dic = new Dictionary<TKey, List<TSource>>(items.Count);
+        var dic = new NullableDictionary<TKey, List<TSource>>(items.Count) { FallbackValue = new List<TSource>() };
         foreach (var item in items)
         {
             var key = keySelector(item);
@@ -1139,10 +1139,10 @@ public static class IDictionaryExtensions
     /// <param name="source"></param>
     /// <param name="keySelector">键选择器</param>
     /// <param name="elementSelector">值选择器</param>
-    public static Dictionary<TKey, List<TElement>> ToLookupX<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
+    public static NullableDictionary<TKey, List<TElement>> ToLookupX<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
     {
         var items = source as IList<TSource> ?? source.ToList();
-        var dic = new Dictionary<TKey, List<TElement>>(items.Count);
+        var dic = new NullableDictionary<TKey, List<TElement>>(items.Count) { FallbackValue = new List<TElement>() };
         foreach (var item in items)
         {
             var key = keySelector(item);
@@ -1168,10 +1168,10 @@ public static class IDictionaryExtensions
     /// <param name="source"></param>
     /// <param name="keySelector">键选择器</param>
     /// <param name="elementSelector">值选择器</param>
-    public static async Task<ConcurrentDictionary<TKey, List<TElement>>> ToLookupAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
+    public static async Task<NullableConcurrentDictionary<TKey, List<TElement>>> ToLookupAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
     {
         var items = source as IList<TSource> ?? source.ToList();
-        var dic = new ConcurrentDictionary<TKey, List<TElement>>();
+        var dic = new NullableConcurrentDictionary<TKey, List<TElement>>(new List<TElement>());
         await items.ForeachAsync(async item =>
         {
             var key = keySelector(item);

+ 21 - 1
Masuit.Tools.Abstractions/Systems/NullableConcurrentDictionary.cs

@@ -123,7 +123,7 @@ public class NullableConcurrentDictionary<TKey, TValue> : ConcurrentDictionary<N
     /// <param name="key"></param>
     public virtual TValue this[TKey key]
     {
-        get => TryGetValue(new NullObject<TKey>(key), out var value) ? value : FallbackValue;
+        get => base.TryGetValue(new NullObject<TKey>(key), out var value) ? value : FallbackValue;
         set => base[new NullObject<TKey>(key)] = value;
     }
 
@@ -132,6 +132,26 @@ public class NullableConcurrentDictionary<TKey, TValue> : ConcurrentDictionary<N
         return base.ContainsKey(new NullObject<TKey>(key));
     }
 
+    public bool TryAdd(TKey key, TValue value)
+    {
+        return base.TryAdd(new NullObject<TKey>(key), value);
+    }
+
+    public bool TryRemove(TKey key, out TValue value)
+    {
+        return base.TryRemove(new NullObject<TKey>(key), out value);
+    }
+
+    public bool TryUpdate(TKey key, TValue value, TValue comparisionValue)
+    {
+        return base.TryUpdate(new NullObject<TKey>(key), value, comparisionValue);
+    }
+
+    public bool TryGetValue(TKey key, out TValue value)
+    {
+        return base.TryGetValue(new NullObject<TKey>(key), out value);
+    }
+
     /// <summary>
     /// 隐式转换
     /// </summary>

+ 20 - 0
Masuit.Tools.Abstractions/Systems/NullableDictionary.cs

@@ -148,6 +148,26 @@ public class NullableDictionary<TKey, TValue> : Dictionary<NullObject<TKey>, TVa
         return base.ContainsKey(new NullObject<TKey>(key));
     }
 
+    public void Add(TKey key, TValue value)
+    {
+        base.Add(new NullObject<TKey>(key), value);
+    }
+
+    public bool Remove(TKey key, out TValue value)
+    {
+        return base.Remove(new NullObject<TKey>(key), out value);
+    }
+
+    public bool Remove(TKey key)
+    {
+        return base.Remove(new NullObject<TKey>(key));
+    }
+
+    public bool TryGetValue(TKey key, out TValue value)
+    {
+        return base.TryGetValue(new NullObject<TKey>(key), out value);
+    }
+
     /// <summary>
     /// 隐式转换
     /// </summary>