/
CastleForwardingInterceptor.cs
45 lines (39 loc) · 1.5 KB
/
CastleForwardingInterceptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using Castle.DynamicProxy;
using NSubstitute.Core;
namespace NSubstitute.Proxies.CastleDynamicProxy
{
public class CastleForwardingInterceptor : IInterceptor
{
private readonly CastleInvocationMapper _invocationMapper;
private readonly ICallRouter _callRouter;
private bool _fullDispatchMode;
public CastleForwardingInterceptor(CastleInvocationMapper invocationMapper, ICallRouter callRouter)
{
_invocationMapper = invocationMapper ?? throw new ArgumentNullException(nameof(invocationMapper));
_callRouter = callRouter ?? throw new ArgumentNullException(nameof(callRouter));
}
public void Intercept(IInvocation invocation)
{
var mappedInvocation = _invocationMapper.Map(invocation);
if (_fullDispatchMode)
{
invocation.ReturnValue = _callRouter.Route(mappedInvocation);
return;
}
// Fallback to the base value until the full dispatch mode is activated.
// Useful to ensure that object is initialized properly.
if (_callRouter.CallBaseByDefault)
{
invocation.ReturnValue = mappedInvocation.TryCallBase().ValueOrDefault();
}
}
/// <summary>
/// Switches interceptor to dispatch calls via the full pipeline.
/// </summary>
public void SwitchToFullDispatchMode()
{
_fullDispatchMode = true;
}
}
}