/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
- using System.Collections.Concurrent;
- using NSubstitute.Exceptions;
- namespace NSubstitute.Core
- {
- public class CallRouterResolver : ICallRouterResolver
- {
- readonly ConcurrentDictionary<object, ICallRouter> _callRouterMappings = new ConcurrentDictionary<object, ICallRouter>();
- public ICallRouter ResolveFor(object substitute)
- {
- if (substitute == null) throw new NullSubstituteReferenceException();
- if (substitute is ICallRouter) return (ICallRouter)substitute;
- if (substitute is ICallRouterProvider) return ((ICallRouterProvider) substitute).CallRouter;
- ICallRouter callRouter;
- if (_callRouterMappings.TryGetValue(substitute, out callRouter))
- {
- return callRouter;
- }
- throw new NotASubstituteException();
- }
- public void Register(object proxy, ICallRouter callRouter)
- {
- if (proxy is ICallRouter) return;
- if (proxy is ICallRouterProvider) return;
- _callRouterMappings.AddOrUpdate(proxy, callRouter, (o,c) => callRouter);
- }
- }
- }