using System; using System.Collections.Concurrent; using System.Linq; namespace Umbraco.Core.ObjectResolution { /// /// Simply used to track all ManyObjectsResolverBase instances so that we can /// reset them all at once really easily. /// /// /// Normally we'd use TypeFinding for this but because many of the resolvers are internal this won't work. /// We'd rather not keep a static list of them so we'll dynamically add to this list based on the base /// class of the ManyObjectsResolverBase. /// internal static class ResolverCollection { private static readonly ConcurrentDictionary Resolvers = new ConcurrentDictionary(); /// /// Returns the number of resolvers created /// internal static int Count { get { return Resolvers.Count; } } /// /// Resets all resolvers /// internal static void ResetAll() { //take out each item from the bag and reset it var keys = Resolvers.Keys.ToArray(); foreach (var k in keys) { Action resetAction; while (Resolvers.TryRemove(k, out resetAction)) { //call the reset action for the resolver resetAction(); } } } /// /// This is called when the static Reset method or a ResolverBase{T} is called. /// internal static void Remove(ResolverBase resolver) { if (resolver == null) return; Action action; Resolvers.TryRemove(resolver, out action); } /// /// Adds a resolver to the collection /// /// /// /// /// This is called when the creation of a ResolverBase occurs /// internal static void Add(ResolverBase resolver, Action resetAction) { Resolvers.TryAdd(resolver, resetAction); } } }