-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.mvvm.cs
277 lines (274 loc) · 12.3 KB
/
Entity.mvvm.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#region Copyright (c) 2000-2021 Developer Express Inc.
/*
{*******************************************************************}
{ }
{ Developer Express .NET Component Library }
{ }
{ }
{ Copyright (c) 2000-2021 Developer Express Inc. }
{ ALL RIGHTS RESERVED }
{ }
{ The entire contents of this file is protected by U.S. and }
{ International Copyright Laws. Unauthorized reproduction, }
{ reverse-engineering, and distribution of all or any portion of }
{ the code contained in this file is strictly prohibited and may }
{ result in severe civil and criminal penalties and will be }
{ prosecuted to the maximum extent possible under the law. }
{ }
{ RESTRICTIONS }
{ }
{ THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES }
{ ARE CONFIDENTIAL AND PROPRIETARY TRADE }
{ SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS }
{ LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET }
{ CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY. }
{ }
{ THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED }
{ FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE }
{ COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE }
{ AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT }
{ AND PERMISSION FROM DEVELOPER EXPRESS INC. }
{ }
{ CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON }
{ ADDITIONAL RESTRICTIONS. }
{ }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2021 Developer Express Inc.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using DevExpress.Mvvm.Native;
using System.Runtime.CompilerServices;
namespace Fx.Common.Entity {
public partial class CEntity {
//---------------------------------------------------------------------
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
protected void NotifyPropertyChanged([CallerMemberName] String propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
//---------------------------------------------------------------------
protected virtual void OnPropertyChanged(string propertyName) {
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
if(PropertyChanged != null)
PropertyChanged(this, e);
}
//---------------------------------------------------------------------
public static string GetPropertyName<T>(Expression<Func<T>> expression) {
/// ???
return GetPropertyNameFast(expression);
}
//---------------------------------------------------------------------
internal static string GetPropertyNameFast(LambdaExpression expression) {
MemberExpression memberExpression = expression.Body as MemberExpression;
if(memberExpression == null) {
throw new ArgumentException("MemberExpression is expected in expression.Body", "expression");
}
const string vblocalPrefix = "$VB$Local_";
var member = memberExpression.Member;
if(
#if !NETFX_CORE
member.MemberType == System.Reflection.MemberTypes.Field &&
#endif
member.Name != null &&
member.Name.StartsWith(vblocalPrefix))
return member.Name.Substring(vblocalPrefix.Length);
return member.Name;
}
public event PropertyChangedEventHandler PropertyChanged;
public T GetProperty<T>(Expression<Func<T>> expression) {
return GetPropertyCore<T>(GetPropertyName(expression));
}
//---------------------------------------------------
public string GetPropertyNameTest() {
var idName = GetProperty(() => Id);
// Good
//var bbb = GetProperty(() => BookmarkName );
return null;
}
//---------------------------------------------------
protected virtual bool SetProperty<T>(ref T storage, T value, string propertyName, Action changedCallback) {
VerifyAccess();
if(CompareValues<T>(storage, value))
return false;
storage = value;
RaisePropertyChanged(propertyName);
changedCallback?.Invoke();
return true;
}
protected bool SetProperty<T>(ref T storage, T value, Expression<Func<T>> expression, Action changedCallback) {
return SetProperty(ref storage, value, GetPropertyName(expression), changedCallback);
}
protected bool SetProperty<T>(ref T storage, T value, Expression<Func<T>> expression) {
return SetProperty<T>(ref storage, value, expression, null);
}
#if !NETFX_CORE
protected bool SetProperty<T>(ref T storage, T value, string propertyName) {
#else
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) {
#endif
return this.SetProperty<T>(ref storage, value, propertyName, null);
}
#if NETFX_CORE
protected T GetProperty<T>([CallerMemberName] string propertyName = null) {
return GetPropertyCore<T>(propertyName);
}
protected bool SetProperty<T>(ref T storage, T value, Action<T, T> changedCallback, [CallerMemberName] string propertyName = null) {
if(object.Equals(storage, value)) return false;
T oldValue = storage;
storage = value;
if(changedCallback != null)
changedCallback(oldValue, value);
RaisePropertiesChanged(propertyName);
return true;
}
protected bool SetProperty<T>(T value, Action changedCallback, [CallerMemberName] string propertyName = null) {
return SetPropertyCore(propertyName, value, changedCallback);
}
protected bool SetProperty<T>(T value, Action<T> changedCallback, [CallerMemberName] string propertyName = null) {
return SetPropertyCore(propertyName, value, changedCallback);
}
protected bool SetProperty<T>(T value, [CallerMemberName] string propertyName = null) {
return SetPropertyCore(propertyName, value, (Action)null);
}
#else
protected T GetValue<T>([CallerMemberName] string propertyName = null) {
GuardPropertyName(propertyName);
return GetPropertyCore<T>(propertyName);
}
protected bool SetValue<T>(T value, [CallerMemberName] string propertyName = null) {
return SetValue(value, default(Action), propertyName);
}
protected bool SetValue<T>(T value, Action changedCallback, [CallerMemberName] string propertyName = null) {
return SetPropertyCore(propertyName, value, changedCallback);
}
protected bool SetValue<T>(T value, Action<T> changedCallback, [CallerMemberName] string propertyName = null) {
return SetPropertyCore(propertyName, value, changedCallback);
}
protected bool SetValue<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) {
return SetValue(ref storage, value, default(Action), propertyName);
}
protected bool SetValue<T>(ref T storage, T value, Action changedCallback, [CallerMemberName] string propertyName = null) {
GuardPropertyName(propertyName);
return SetProperty(ref storage, value, propertyName, changedCallback);
}
static void GuardPropertyName(string propertyName) {
if(string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException(nameof(propertyName));
}
#endif
protected bool SetProperty<T>(Expression<Func<T>> expression, T value) {
return SetProperty(expression, value, (Action)null);
}
protected bool SetProperty<T>(Expression<Func<T>> expression, T value, Action changedCallback) {
string propertyName = GetPropertyName(expression);
return SetPropertyCore(propertyName, value, changedCallback);
}
protected bool SetProperty<T>(Expression<Func<T>> expression, T value, Action<T> changedCallback) {
string propertyName = GetPropertyName(expression);
return SetPropertyCore(propertyName, value, changedCallback);
}
#region RaisePropertyChanged
#if !NETFX_CORE
protected void RaisePropertyChanged(string propertyName) {
#else
protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) {
#endif
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#if !NETFX_CORE
protected void RaisePropertyChanged() {
RaisePropertiesChanged(null);
}
#endif
protected void RaisePropertyChanged<T>(Expression<Func<T>> expression) {
RaisePropertyChanged(GetPropertyName(expression));
}
protected void RaisePropertiesChanged(params string[] propertyNames) {
if(propertyNames == null || propertyNames.Length == 0) {
RaisePropertyChanged(string.Empty);
return;
}
foreach(string propertyName in propertyNames) {
RaisePropertyChanged(propertyName);
}
}
protected void RaisePropertiesChanged<T1, T2>(Expression<Func<T1>> expression1, Expression<Func<T2>> expression2) {
RaisePropertyChanged(expression1);
RaisePropertyChanged(expression2);
}
protected void RaisePropertiesChanged<T1, T2, T3>(Expression<Func<T1>> expression1, Expression<Func<T2>> expression2, Expression<Func<T3>> expression3) {
RaisePropertyChanged(expression1);
RaisePropertyChanged(expression2);
RaisePropertyChanged(expression3);
}
protected void RaisePropertiesChanged<T1, T2, T3, T4>(Expression<Func<T1>> expression1, Expression<Func<T2>> expression2, Expression<Func<T3>> expression3, Expression<Func<T4>> expression4) {
RaisePropertyChanged(expression1);
RaisePropertyChanged(expression2);
RaisePropertyChanged(expression3);
RaisePropertyChanged(expression4);
}
protected void RaisePropertiesChanged<T1, T2, T3, T4, T5>(Expression<Func<T1>> expression1, Expression<Func<T2>> expression2, Expression<Func<T3>> expression3, Expression<Func<T4>> expression4, Expression<Func<T5>> expression5) {
RaisePropertyChanged(expression1);
RaisePropertyChanged(expression2);
RaisePropertyChanged(expression3);
RaisePropertyChanged(expression4);
RaisePropertyChanged(expression5);
}
#endregion
#region property bag
Dictionary<string, object> _propertyBag;
Dictionary<string, object> PropertyBag => _propertyBag ?? (_propertyBag = new Dictionary<string, object>());
#if DEBUG
internal Dictionary<string, object> PropertyBagForTests => PropertyBag;
#endif
T GetPropertyCore<T>(string propertyName) {
object val;
if(PropertyBag.TryGetValue(propertyName, out val))
return (T)val;
return default(T);
}
bool SetPropertyCore<T>(string propertyName, T value, Action changedCallback) {
T oldValue;
var res = SetPropertyCore(propertyName, value, out oldValue);
if(res) {
changedCallback?.Invoke();
}
return res;
}
bool SetPropertyCore<T>(string propertyName, T value, Action<T> changedCallback) {
T oldValue;
var res = SetPropertyCore(propertyName, value, out oldValue);
if(res) {
changedCallback?.Invoke(oldValue);
}
return res;
}
protected virtual bool SetPropertyCore<T>(string propertyName, T value, out T oldValue) {
VerifyAccess();
oldValue = default(T);
object val;
if(PropertyBag.TryGetValue(propertyName, out val))
oldValue = (T)val;
if(CompareValues<T>(oldValue, value))
return false;
lock(PropertyBag) {
PropertyBag[propertyName] = value;
}
RaisePropertyChanged(propertyName);
return true;
}
protected virtual void VerifyAccess() {
}
static bool CompareValues<T>(T storage, T value) {
return EqualityComparer<T>.Default.Equals(storage, value);
}
#endregion
}
}