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 value; if (!dictionary.TryGetValue(key, out value)) { value = factory(key); dictionary.Add(key, value); } return value; } public static TValue GetOrAdd( this IDictionary dictionary, TKey key, Func factory) { TValue value; if (!dictionary.TryGetValue(key, out value)) { value = factory(); dictionary.Add(key, value); } return value; } public static TValue AddOrUpdate( this IDictionary dictionary, TKey key, Func addFactory, Func updateFactory) { TValue value; if (dictionary.TryGetValue(key, out value)) { value = updateFactory(key, value); } else { value = addFactory(key); } dictionary[key] = value; return value; } public static TValue AddOrUpdate( this IDictionary dictionary, TKey key, Func addFactory, Func updateFactory) { TValue value; if (dictionary.TryGetValue(key, out value)) { value = updateFactory(value); } else { value = addFactory(); } dictionary[key] = value; return value; } } }