/Source/NSubstitute/Core/CallRouterResolver.cs

http://github.com/nsubstitute/NSubstitute · C# · 31 lines · 27 code · 4 blank · 0 comment · 7 complexity · 41e26fe59d85f4fb76c5b9062e6e115d MD5 · raw file

  1. using System.Collections.Concurrent;
  2. using NSubstitute.Exceptions;
  3. namespace NSubstitute.Core
  4. {
  5. public class CallRouterResolver : ICallRouterResolver
  6. {
  7. readonly ConcurrentDictionary<object, ICallRouter> _callRouterMappings = new ConcurrentDictionary<object, ICallRouter>();
  8. public ICallRouter ResolveFor(object substitute)
  9. {
  10. if (substitute == null) throw new NullSubstituteReferenceException();
  11. if (substitute is ICallRouter) return (ICallRouter)substitute;
  12. if (substitute is ICallRouterProvider) return ((ICallRouterProvider) substitute).CallRouter;
  13. ICallRouter callRouter;
  14. if (_callRouterMappings.TryGetValue(substitute, out callRouter))
  15. {
  16. return callRouter;
  17. }
  18. throw new NotASubstituteException();
  19. }
  20. public void Register(object proxy, ICallRouter callRouter)
  21. {
  22. if (proxy is ICallRouter) return;
  23. if (proxy is ICallRouterProvider) return;
  24. _callRouterMappings.AddOrUpdate(proxy, callRouter, (o,c) => callRouter);
  25. }
  26. }
  27. }