-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
PropertyChangedInterceptor.cs
117 lines (103 loc) · 4.62 KB
/
PropertyChangedInterceptor.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.ComponentModel;
using Castle.DynamicProxy;
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 PropertyChangedInterceptor : PropertyChangeInterceptorBase, IInterceptor
{
private static readonly Type NotifyChangedInterface = typeof(INotifyPropertyChanged);
private readonly bool _checkEquality;
private PropertyChangedEventHandler? _handler;
/// <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 PropertyChangedInterceptor(
IEntityType entityType,
bool checkEquality)
: base(entityType)
=> _checkEquality = checkEquality;
/// <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 void Intercept(IInvocation invocation)
{
var methodName = invocation.Method.Name;
if (invocation.Method.DeclaringType == NotifyChangedInterface)
{
if (methodName == $"add_{nameof(INotifyPropertyChanged.PropertyChanged)}")
{
_handler = (PropertyChangedEventHandler)Delegate.Combine(
_handler, (Delegate)invocation.Arguments[0]);
}
else if (methodName == $"remove_{nameof(INotifyPropertyChanged.PropertyChanged)}")
{
_handler = (PropertyChangedEventHandler?)Delegate.Remove(
_handler, (Delegate)invocation.Arguments[0]);
}
}
else if (methodName.StartsWith("set_", StringComparison.Ordinal))
{
var propertyName = FindPropertyName(invocation);
var property = EntityType.FindProperty(propertyName);
if (property != null)
{
HandleChanged(invocation, property, GetValueComparer(property));
}
else
{
var navigation = EntityType.FindNavigation(propertyName)
?? (INavigationBase?)EntityType.FindSkipNavigation(propertyName);
if (navigation != null)
{
HandleChanged(invocation, navigation, ReferenceEqualityComparer.Instance);
}
else
{
invocation.Proceed();
}
}
}
else
{
invocation.Proceed();
}
}
private void HandleChanged(IInvocation invocation, IPropertyBase property, IEqualityComparer? comparer)
{
var newValue = invocation.Arguments[^1];
if (_checkEquality)
{
var oldValue = property.GetGetter().GetClrValueUsingContainingEntity(invocation.Proxy);
invocation.Proceed();
if (!(comparer?.Equals(oldValue, newValue) ?? Equals(oldValue, newValue)))
{
NotifyPropertyChanged(property.Name, invocation.Proxy);
}
else
{
invocation.Proceed();
}
}
else
{
invocation.Proceed();
NotifyPropertyChanged(property.Name, invocation.Proxy);
}
}
private void NotifyPropertyChanged(string propertyName, object proxy)
=> _handler?.Invoke(proxy, new PropertyChangedEventArgs(propertyName));
}