-
Notifications
You must be signed in to change notification settings - Fork 22
/
AdvancedInterceptionScenario.cs
141 lines (121 loc) · 3.58 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
141
/*
$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
namespace Pure.DI.UsageTests.Interception.AdvancedInterceptionScenario;
using System.Collections.Immutable;
using System.Linq.Expressions;
using Castle.DynamicProxy;
using Shouldly;
using Xunit;
// {
public interface IDependency
{
void DependencyCall();
}
class Dependency : IDependency
{
public void DependencyCall()
{
}
}
public interface IService
{
IDependency Dependency { get; }
void ServiceCall();
}
class Service(IDependency dependency) : IService
{
public IDependency Dependency { get; } = dependency;
public void ServiceCall()
{
}
}
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];
}
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)
{
_log.Add(invocation.Method.Name);
invocation.Proceed();
}
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.ServiceCall();
service.Dependency.DependencyCall();
log.ShouldBe(
ImmutableArray.Create(
"ServiceCall",
"get_Dependency",
"DependencyCall"));
// }
composition.SaveClassDiagram();
}
}