/
BasePropertyValidation.cs
executable file
·335 lines (290 loc) · 12.5 KB
/
BasePropertyValidation.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// <copyright file="ReactiveUI.Validation/src/ReactiveUI.Validation/Components/BasePropertyValidation.cs" company=".NET Foundation">
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// </copyright>
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using ReactiveUI.Validation.Collections;
using ReactiveUI.Validation.Comparators;
using ReactiveUI.Validation.Components.Abstractions;
using ReactiveUI.Validation.States;
namespace ReactiveUI.Validation.Components
{
/// <inheritdoc cref="ReactiveObject" />
/// <inheritdoc cref="IDisposable" />
/// <inheritdoc cref="IValidationComponent" />
/// <summary>
/// Base class for items which are used to build a <see cref="ReactiveUI.Validation.Contexts.ValidationContext" />.
/// </summary>
public abstract class BasePropertyValidation<TViewModel> : ReactiveObject, IDisposable, IValidationComponent
{
/// <summary>
/// The current valid state.
/// </summary>
private readonly ReplaySubject<bool> _isValidSubject = new ReplaySubject<bool>(1);
/// <summary>
/// The list of property names this validator.
/// </summary>
private readonly HashSet<string> _propertyNames = new HashSet<string>();
/// <summary>
/// The items to be disposed.
/// </summary>
private CompositeDisposable _disposables = new CompositeDisposable();
/// <summary>
/// The connected observable to kick off seeing <see cref="ValidationStatusChange" />.
/// </summary>
private IConnectableObservable<ValidationState> _connectedChange;
private bool _isConnected;
/// <summary>
/// Our current validity state.
/// </summary>
private bool _isValid;
private ValidationText _text;
/// <summary>
/// Initializes a new instance of the <see cref="BasePropertyValidation{TViewModel}"/> class.
/// </summary>
protected BasePropertyValidation()
{
// subscribe to the valid subject so we can assign the validity
_disposables.Add(_isValidSubject.Subscribe(v => _isValid = v));
}
/// <summary>
/// Gets get the total number of properties referenced.
/// </summary>
public int PropertyCount => _propertyNames.Count;
/// <inheritdoc />
public bool IsValid
{
get
{
Activate();
return _isValid;
}
}
/// <summary>
/// Gets the public mechanism indicating that the validation state has changed.
/// </summary>
public IObservable<ValidationState> ValidationStatusChange
{
get
{
Activate();
return _connectedChange;
}
}
/// <inheritdoc/>
public ValidationText Text
{
get
{
Activate();
return _text;
}
}
/// <inheritdoc/>
public void Dispose()
{
// Dispose of unmanaged resources.
Dispose(true);
// Suppress finalization.
GC.SuppressFinalize(this);
}
/// <summary>
/// Determine if a property name is actually contained within this.
/// </summary>
/// <typeparam name="TProp">Any type.</typeparam>
/// <param name="property">ViewModel property.</param>
/// <param name="exclusively">Indicates if the property to find is unique.</param>
/// <returns>Returns true if it contains the property, otherwise false.</returns>
public bool ContainsProperty<TProp>(Expression<Func<TViewModel, TProp>> property, bool exclusively = false)
{
var propertyName = property.Body.GetMemberInfo().ToString();
return exclusively
? _propertyNames.Contains(propertyName) && _propertyNames.Count == 1
: _propertyNames.Contains(propertyName);
}
/// <summary>
/// Adds a property to the list of this which this validation is associated with.
/// </summary>
/// <typeparam name="TProp">Any type.</typeparam>
/// <param name="property">ViewModel property.</param>
protected void AddProperty<TProp>(Expression<Func<TViewModel, TProp>> property)
{
var propertyName = property.Body.GetMemberInfo().ToString();
_propertyNames.Add(propertyName);
}
/// <summary>
/// Get the validation change observable, implemented by concrete classes.
/// </summary>
/// <returns>Returns the <see cref="ValidationState"/> collection.</returns>
protected abstract IObservable<ValidationState> GetValidationChangeObservable();
/// <summary>
/// Disposes of the managed resources.
/// </summary>
/// <param name="disposing">If its getting called by the <see cref="BasePropertyValidation{TViewModel}.Dispose()"/> method.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_disposables?.Dispose();
_disposables = null;
}
}
private void Activate()
{
if (_isConnected)
{
return;
}
_connectedChange = GetValidationChangeObservable()
.Do(state =>
{
_isValid = state.IsValid;
_text = state.Text;
})
.Replay(1);
_disposables.Add(_connectedChange.Connect());
_isConnected = true;
}
}
/// <inheritdoc />
/// <summary>
/// Property validator for a single view model property.
/// </summary>
/// <typeparam name="TViewModel"></typeparam>
/// <typeparam name="TViewModelProperty"></typeparam>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification = "Same class just generic.")]
public sealed class BasePropertyValidation<TViewModel, TViewModelProperty> : BasePropertyValidation<TViewModel>
{
/// <summary>
/// The message to be constructed.
/// </summary>
private readonly Func<TViewModelProperty, bool, ValidationText> _message;
private readonly IConnectableObservable<TViewModelProperty> _valueConnectedObservable;
/// <summary>
/// The value calculated from the properties.
/// </summary>
private readonly ReplaySubject<TViewModelProperty> _valueSubject = new ReplaySubject<TViewModelProperty>(1);
private CompositeDisposable _disposables = new CompositeDisposable();
private bool _isConnected;
/// <summary>
/// Initializes a new instance of the <see cref="BasePropertyValidation{TViewModel, TProperty1}"/> class.
/// </summary>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModelProperty">ViewModel property.</param>
/// <param name="isValidFunc">Func to define if the viewModelProperty is valid or not.</param>
/// <param name="message">Validation error message.</param>
public BasePropertyValidation(
TViewModel viewModel,
Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
Func<TViewModelProperty, bool> isValidFunc,
string message)
: this(viewModel, viewModelProperty, isValidFunc, (p, v) => new ValidationText(v
? string.Empty
: message))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BasePropertyValidation{TViewModel, TViewModelProperty}"/> class.
/// </summary>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModelProperty">ViewModel property.</param>
/// <param name="isValidFunc">Func to define if the viewModelProperty is valid or not.</param>
/// <param name="message">Func to define the validation error message based on the viewModelProperty value.</param>
public BasePropertyValidation(
TViewModel viewModel,
Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
Func<TViewModelProperty, bool> isValidFunc,
Func<TViewModelProperty, string> message)
: this(viewModel, viewModelProperty, isValidFunc, (p, v) => new ValidationText(v
? string.Empty
: message(p)))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BasePropertyValidation{TViewModel, TViewModelProperty}"/> class.
/// </summary>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModelProperty">ViewModel property.</param>
/// <param name="isValidFunc">Func to define if the viewModelProperty is valid or not.</param>
/// <param name="messageFunc">Func to define the validation error message based on the viewModelProperty and isValidFunc values.</param>
public BasePropertyValidation(
TViewModel viewModel,
Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
Func<TViewModelProperty, bool> isValidFunc,
Func<TViewModelProperty, bool, string> messageFunc)
: this(viewModel, viewModelProperty, isValidFunc, (prop1, isValid) =>
new ValidationText(messageFunc(prop1, isValid)))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BasePropertyValidation{TViewModel, TViewModelProperty}"/> class.
/// Main constructor.
/// </summary>
/// <param name="viewModel">ViewModel instance.</param>
/// <param name="viewModelProperty">ViewModel property.</param>
/// <param name="isValidFunc">Func to define if the viewModelProperty is valid or not.</param>
/// <param name="messageFunc">Func to define the validation error message based on the viewModelProperty and isValidFunc values.</param>
private BasePropertyValidation(
TViewModel viewModel,
Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty,
Func<TViewModelProperty, bool> isValidFunc,
Func<TViewModelProperty, bool, ValidationText> messageFunc)
{
// Now, we have a function, which, in this case uses the value of the view Model Property...
IsValidFunc = isValidFunc;
// Record this property name
AddProperty(viewModelProperty);
// The function invoked
_message = messageFunc;
// Our connected observable
_valueConnectedObservable = viewModel.WhenAny(viewModelProperty, v => v.Value).DistinctUntilChanged()
.Multicast(_valueSubject);
}
/// <summary>
/// Gets the mechanism to determine if the property(s) is valid or not.
/// </summary>
private Func<TViewModelProperty, bool> IsValidFunc { get; }
/// <inheritdoc />
/// <summary>
/// Get the validation change observable.
/// </summary>
/// <returns></returns>
protected override IObservable<ValidationState> GetValidationChangeObservable()
{
Activate();
return _valueSubject.Select(value => new ValidationState(IsValidFunc(value), GetMessage(value), this))
.DistinctUntilChanged(new ValidationStateComparer());
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
_disposables?.Dispose();
_disposables = null;
}
}
private void Activate()
{
if (_isConnected)
{
return;
}
_disposables.Add(_valueConnectedObservable.Connect());
_isConnected = true;
}
private ValidationText GetMessage(TViewModelProperty value)
{
// Need something subtle to deal with validity having not actual message
return _message(value, IsValidFunc(value));
}
}
}