// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. namespace System.Data.Entity { using System; using System.Collections.Generic; using System.Data.Entity.Config; /// /// A resolver that allows to add dependency resolvers at runtime. /// /// /// This class isn't thread-safe as the tests using it aren't expected to be run in parallel. /// public class MutableResolver : IDbDependencyResolver { private static readonly Dictionary> _resolvers = new Dictionary>(); private static readonly MutableResolver _instance = new MutableResolver(); private MutableResolver() { } /// public object GetService(Type type, object key) { Func resolver; if (_resolvers.TryGetValue(type, out resolver)) { return resolver(key); } return null; } public static MutableResolver Instance { get { return _instance; } } /// /// Adds or replaces a resolver for a dependency of type . /// /// /// Remember to call from a finally block after using this method. /// /// The type of dependency to resolve. /// A delegate that takes a key object and returns a dependency instance. public static void AddResolver(Func resolver) { _resolvers.Add(typeof(TResolver), resolver); } /// /// Removes all added resolvers. /// public static void ClearResolvers() { _resolvers.Clear(); } } }