using System; using System.Collections.Generic; namespace Admin.Core.Common.Extensions { public static class DictionaryExtensions { public static TValue GetOrAdd( this IDictionary dictionary, TKey key, Func factory) { TValue obj; if (dictionary.TryGetValue(key, out obj)) { return obj; } return dictionary[key] = factory(key); } public static TValue GetOrAdd( this IDictionary dictionary, TKey key, Func factory) { return dictionary.GetOrAdd(key, k => factory()); } public static TValue AddOrUpdate( this IDictionary dictionary, TKey key, Func addFactory, Func updateFactory) { TValue obj; if (dictionary.TryGetValue(key, out obj)) { obj = updateFactory(key, obj); } else { obj = addFactory(key); } dictionary[key] = obj; return obj; } public static TValue AddOrUpdate( this IDictionary dictionary, TKey key, Func addFactory, Func updateFactory) { return dictionary.AddOrUpdate(key, k => addFactory(), (k, v) => updateFactory(v)); } } }