-
Notifications
You must be signed in to change notification settings - Fork 22
/
AdvancedInterceptionScenario.cs
140 lines (120 loc) · 3.82 KB
/
AdvancedInterceptionScenario.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
$v=true
$p=2
$d=Advanced interception
$h=This approach of interception maximizes performance by precompiling the proxy object factory.
*/
// ReSharper disable ClassNeverInstantiated.Local
// ReSharper disable CheckNamespace
// ReSharper disable UnusedParameterInPartialMethod
// ReSharper disable ConvertIfStatementToReturnStatement
// ReSharper disable ArrangeTypeModifiers
// ReSharper disable UnusedMethodReturnValue.Global
namespace Pure.DI.UsageTests.Interception.AdvancedInterceptionScenario;
using System.Collections.Immutable;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using Castle.DynamicProxy;
using Shouldly;
using Xunit;
// {
public interface IDependency
{
int DependencyRun();
}
class Dependency : IDependency
{
public int DependencyRun() => 33;
}
public interface IService
{
IDependency Dependency { get; }
string ServiceRun();
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency { get; } = dependency;
public string ServiceRun() => "Abc";
}
internal partial class Composition : IInterceptor
{
private readonly List<string> _log = [];
private static readonly IProxyBuilder ProxyBuilder = new DefaultProxyBuilder();
private readonly IInterceptor[] _interceptors = [];
public Composition(List<string> log)
: this()
{
_log = log;
_interceptors = [this];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private partial T OnDependencyInjection<T>(
in T value,
object? tag,
Lifetime lifetime)
{
if (typeof(T).IsValueType)
{
return value;
}
return ProxyFactory<T>.GetFactory(ProxyBuilder)(
value,
_interceptors);
}
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
_log.Add($"{invocation.Method.Name} returns {invocation.ReturnValue}");
}
private static class ProxyFactory<T>
{
private static Func<T, IInterceptor[], T>? _factory;
public static Func<T, IInterceptor[], T> GetFactory(IProxyBuilder proxyBuilder) =>
_factory ?? CreateFactory(proxyBuilder);
private static Func<T, IInterceptor[], T> CreateFactory(IProxyBuilder proxyBuilder)
{
// Compiles a delegate to create a proxy for the performance boost
var proxyType = proxyBuilder.CreateInterfaceProxyTypeWithTargetInterface(
typeof(T),
Type.EmptyTypes,
ProxyGenerationOptions.Default);
var ctor = proxyType.GetConstructors()
.Single(i => i.GetParameters().Length == 2);
var instance = Expression.Parameter(typeof(T));
var interceptors = Expression.Parameter(typeof(IInterceptor[]));
var newProxyExpression = Expression.New(ctor, interceptors, instance);
return _factory = Expression.Lambda<Func<T, IInterceptor[], T>>(
newProxyExpression,
instance,
interceptors)
.Compile();
}
}
}
// }
public class Scenario
{
[Fact]
public void Run()
{
// Resolve = Off
// {
// OnDependencyInjection = On
DI.Setup(nameof(Composition))
.Bind().To<Dependency>()
.Bind().To<Service>()
.Root<IService>("Root");
var log = new List<string>();
var composition = new Composition(log);
var service = composition.Root;
service.ServiceRun();
service.Dependency.DependencyRun();
log.ShouldBe(
ImmutableArray.Create(
"ServiceRun returns Abc",
"get_Dependency returns Castle.Proxies.IDependencyProxy",
"DependencyRun returns 33"));
// }
composition.SaveClassDiagram();
}
}