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 |
#region Using Statements
using System.Collections.Generic;
using System.Linq;
#endregion
namespace StructuredWeb.ValidationFramework.Result
{
public sealed class ValidationResult
{
private bool _dirty = true;
private bool _valid;
public ValidationResult()
{
Messages = new List<ValidationMessage>();
}
public bool Valid
{
get
{
//theoretically O(n) time but in pratice will be constant time
//Still, no need for multiple traversals, Traverse only if it's changed
if (_dirty)
{
_valid = Messages.Count(msg => msg.Warning == false) == 0;
_dirty = false;
}
return _valid;
}
}
public IList<ValidationMessage> Messages { get; internal set; }
/// <summary>
/// Adds the error. Allows users to add custom messages after validation.
/// Enterprise Library validation doesn't allow this which can cause the need
/// to marshal message objects around instead of being able to bind this collection.
/// </summary>
/// <param name="errorMessage">The error message.</param>
public void AddError(string errorMessage)
{
_dirty = true;
Messages.Add(new ValidationMessage {Message = errorMessage});
}
/// <summary>
/// Adds the warning. Allows users to add custom messages after validation.
/// Enterprise Library validation doesn't allow this which can cause the need
/// to marshal message objects around instead of being able to bind this collection.
/// </summary>
/// <param name="warningMessage">The warning message.</param>
public void AddWarning(string warningMessage)
{
//No need to mark collection dirty since warnings never generate validation changes
Messages.Add(new ValidationMessage {Message = warningMessage, Warning = true});
}
}
} |