-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
ProxyFactory.cs
196 lines (169 loc) · 8 KB
/
ProxyFactory.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using Castle.DynamicProxy;
using Microsoft.EntityFrameworkCore.Internal;
using IInterceptor = Castle.DynamicProxy.IInterceptor;
namespace Microsoft.EntityFrameworkCore.Proxies.Internal;
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class ProxyFactory : IProxyFactory
{
private static readonly Type ProxyLazyLoaderInterface = typeof(IProxyLazyLoader);
private static readonly Type NotifyPropertyChangedInterface = typeof(INotifyPropertyChanged);
private static readonly Type NotifyPropertyChangingInterface = typeof(INotifyPropertyChanging);
private static readonly ProxyGenerationOptions GenerationOptions = new(new ClonelessProxyGenerationHook());
private readonly ProxyGenerator _generator = new();
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual object Create(
DbContext context,
Type type,
params object[] constructorArguments)
{
var entityType = context.Model.FindRuntimeEntityType(type);
if (entityType == null)
{
if (context.Model.IsShared(type))
{
throw new InvalidOperationException(ProxiesStrings.EntityTypeNotFoundShared(type.ShortDisplayName()));
}
throw new InvalidOperationException(CoreStrings.EntityTypeNotFound(type.ShortDisplayName()));
}
return CreateProxy(context, entityType, constructorArguments);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Type CreateProxyType(
IEntityType entityType)
=> _generator.ProxyBuilder.CreateClassProxyType(
entityType.ClrType,
GetInterfacesToProxy(entityType),
GenerationOptions);
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual object CreateLazyLoadingProxy(
DbContext context,
IEntityType entityType,
ILazyLoader loader,
object[] constructorArguments)
{
var options = context.GetService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>();
if (options == null)
{
throw new InvalidOperationException(ProxiesStrings.ProxyServicesMissing);
}
return CreateLazyLoadingProxy(entityType, loader, constructorArguments);
}
private object CreateLazyLoadingProxy(
IEntityType entityType,
ILazyLoader loader,
object[] constructorArguments)
=> _generator.CreateClassProxy(
entityType.ClrType,
GetInterfacesToProxy(entityType),
GenerationOptions,
constructorArguments,
GetNotifyChangeInterceptors(entityType, new LazyLoadingInterceptor(entityType, loader)));
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual object CreateProxy(
DbContext context,
IEntityType entityType,
object[] constructorArguments)
{
var options = context.GetService<IDbContextOptions>().FindExtension<ProxiesOptionsExtension>();
if (options == null)
{
throw new InvalidOperationException(ProxiesStrings.ProxyServicesMissing);
}
if ((bool?)entityType.Model[ProxyAnnotationNames.LazyLoading] == true)
{
return CreateLazyLoadingProxy(
entityType,
context.GetService<ILazyLoader>(),
constructorArguments);
}
return CreateProxy(
entityType,
constructorArguments);
}
private object CreateProxy(
IEntityType entityType,
object[] constructorArguments)
=> _generator.CreateClassProxy(
entityType.ClrType,
GetInterfacesToProxy(entityType),
GenerationOptions,
constructorArguments,
GetNotifyChangeInterceptors(entityType));
private static Type[] GetInterfacesToProxy(IReadOnlyEntityType entityType)
{
var interfacesToProxy = new List<Type>();
if ((bool?)entityType.Model[ProxyAnnotationNames.LazyLoading] == true)
{
interfacesToProxy.Add(ProxyLazyLoaderInterface);
}
if ((bool?)entityType.Model[ProxyAnnotationNames.ChangeTracking] == true)
{
if (!NotifyPropertyChangedInterface.IsAssignableFrom(entityType.ClrType))
{
interfacesToProxy.Add(NotifyPropertyChangedInterface);
}
if (!NotifyPropertyChangingInterface.IsAssignableFrom(entityType.ClrType))
{
interfacesToProxy.Add(NotifyPropertyChangingInterface);
}
}
return interfacesToProxy.ToArray();
}
private static IInterceptor[] GetNotifyChangeInterceptors(
IEntityType entityType,
LazyLoadingInterceptor? lazyLoadingInterceptor = null)
{
var interceptors = new List<IInterceptor>();
if (lazyLoadingInterceptor != null)
{
interceptors.Add(lazyLoadingInterceptor);
}
if ((bool?)entityType.Model[ProxyAnnotationNames.ChangeTracking] == true)
{
var checkEquality = (bool?)entityType.Model[ProxyAnnotationNames.CheckEquality] == true;
if (!NotifyPropertyChangedInterface.IsAssignableFrom(entityType.ClrType))
{
interceptors.Add(new PropertyChangedInterceptor(entityType, checkEquality));
}
if (!NotifyPropertyChangingInterface.IsAssignableFrom(entityType.ClrType))
{
interceptors.Add(new PropertyChangingInterceptor(entityType, checkEquality));
}
}
return interceptors.ToArray();
}
private sealed class ClonelessProxyGenerationHook : AllMethodsHook
{
public override bool ShouldInterceptMethod(Type type, MethodInfo methodInfo)
=> methodInfo.Name != "<Clone>$"
&& base.ShouldInterceptMethod(type, methodInfo);
}
}