CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BrokenRulesCollection.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BrokenRulesCollection.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>A collection of currently broken rules.</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Linq;
10using System.Collections.Generic;
11using Csla.Properties;
13
14namespace Csla.Rules
15{
26 public class BrokenRulesCollection : Core.ReadOnlyObservableBindingList<BrokenRule>
27 {
28 private int _errorCount;
29 private int _warnCount;
30 private int _infoCount;
31
32 private object _syncRoot = new object();
33
34
40 : this(false)
41 { }
42
43 internal BrokenRulesCollection(bool readOnly)
44 {
45 IsReadOnly = readOnly;
46 }
47
48 internal void ClearRules()
49 {
50 lock (_syncRoot)
51 {
52 IsReadOnly = false;
53 base.Clear();
54 _errorCount = _warnCount = _infoCount = 0;
55 IsReadOnly = true;
56 }
57 }
58
59 internal void ClearRules(Csla.Core.IPropertyInfo property)
60 {
61 lock (_syncRoot)
62 {
63 this.IsReadOnly = false;
64
65 var propertyName = property == null ? null : property.Name;
66 for (int i = 0, n = Count; i < n; i++)
67 {
68 var x = this[i];
69 if (x.OriginProperty == propertyName)
70 {
71 RemoveAt(i);
72 i--;
73 n--;
74 }
75 }
76
77 this.IsReadOnly = true;
78 }
79 }
80
81 internal void SetBrokenRules(List<RuleResult> results, string originPropertyName)
82 {
83 lock (_syncRoot)
84 {
85 this.IsReadOnly = false;
86
87 ISet<string> rulesDone = new HashSet<string>();
88
89 for(int i = 0, n = results.Count; i < n; i++)
90 {
91 var result = results[i];
92 var resultRuleName = result.RuleName;
93
94 if(!rulesDone.Contains(resultRuleName))
95 {
96 rulesDone.Add(resultRuleName);
97
98 ClearRules(resultRuleName, originPropertyName);
99 }
100
101 if(result.Success)
102 continue;
103
104 var resultDescription = result.Description;
105
106 if(string.IsNullOrEmpty(resultDescription))
107 throw new ArgumentException(string.Format(Resources.RuleMessageRequired,
108 resultRuleName));
109
110 var resultPrimaryProperty = result.PrimaryProperty;
111
112 BrokenRule broken = new BrokenRule
113 {
114 RuleName = resultRuleName,
115 Description = resultDescription,
116 Property = resultPrimaryProperty == null
117 ? null : resultPrimaryProperty.Name,
118 Severity = result.Severity,
119 OriginProperty = originPropertyName
120 };
121
122 Add(broken);
123 }
124
125 this.IsReadOnly = true;
126 }
127 }
128
132 private void ClearRules(string ruleName, string originProperty)
133 {
134 for(int i = 0, n = Count; i < n; i++)
135 {
136 var x = this[i];
137
138 if(x.RuleName == ruleName && x.OriginProperty == originProperty)
139 {
140 RemoveAt(i);
141 i--;
142 n--;
143 }
144 }
145 }
146
147 new void RemoveAt(int i)
148 {
149 CountOne(this[i].Severity, -1);
150
151 base.RemoveAt(i);
152 }
153
154 new void Add(BrokenRule item)
155 {
156 base.Add(item);
157
158 CountOne(item.Severity, 1);
159 }
160
161 private void CountOne(RuleSeverity severity, int one)
162 {
163 switch (severity)
164 {
165 case RuleSeverity.Error:
166 _errorCount += one;
167 break;
168 case RuleSeverity.Warning:
169 _warnCount += one;
170 break;
171 case RuleSeverity.Information:
172 _infoCount += one;
173 break;
174 default:
175 throw new Exception("unhandled severity=" + severity);
176 }
177 }
178
185 public int ErrorCount
186 {
187 get { return _errorCount; }
188 }
189
196 public int WarningCount
197 {
198 get { return _warnCount; }
199 }
200
208 {
209 get { return _infoCount; }
210 }
211
227 {
228 return GetFirstMessage(property.Name, RuleSeverity.Error);
229 }
230
245 public BrokenRule GetFirstBrokenRule(string property)
246 {
247 return GetFirstMessage(property, RuleSeverity.Error);
248 }
249
265 {
266 return this.Where(c => c.Property == property.Name).FirstOrDefault();
267 }
268
281 {
282 return GetFirstMessage(property.Name, severity);
283 }
284
296 public BrokenRule GetFirstMessage(string property, RuleSeverity severity)
297 {
298 return this.Where(c => c.Property == property && c.Severity == severity).FirstOrDefault();
299 }
300
306 public override string ToString()
307 {
308 return ToString(Environment.NewLine);
309 }
310
320 public string ToString(RuleSeverity severity)
321 {
322 return ToString(Environment.NewLine, severity);
323 }
324
332 public string ToString(string separator)
333 {
334 System.Text.StringBuilder result = new System.Text.StringBuilder();
335 bool first = true;
336 foreach (BrokenRule item in this)
337 {
338 if (first)
339 first = false;
340 else
341 result.Append(separator);
342 result.Append(item.Description);
343 }
344 return result.ToString();
345 }
346
358 public string ToString(string separator, RuleSeverity severity)
359 {
360 System.Text.StringBuilder result = new System.Text.StringBuilder();
361 bool first = true;
362 foreach (BrokenRule item in this)
363 {
364 if (item.Severity == severity)
365 {
366 if (first)
367 first = false;
368 else
369 result.Append(separator);
370 result.Append(item.Description);
371 }
372 }
373 return result.ToString();
374 }
375
388 public string ToString(string separator, RuleSeverity severity, string propertyName)
389 {
390 System.Text.StringBuilder result = new System.Text.StringBuilder();
391 bool first = true;
392 foreach (BrokenRule item in this.Where(r => r.Property == propertyName))
393 {
394 if (item.Severity == severity)
395 {
396 if (first)
397 first = false;
398 else
399 result.Append(separator);
400 result.Append(item.Description);
401 }
402 }
403 return result.ToString();
404 }
405
412 public string[] ToArray()
413 {
414 return this.Select(c => c.Description).ToArray();
415 }
416
425 public string[] ToArray(RuleSeverity severity)
426 {
427 return this.Where(c => c.Severity == severity).Select(c => c.Description).ToArray();
428 }
429
434 public void AddRange(List<BrokenRule> list)
435 {
436 foreach (var item in list)
437 Add(item);
438 }
439
447 protected override void OnGetState(SerializationInfo info)
448 {
449 info.AddValue("_errorCount", _errorCount);
450 info.AddValue("_warnCount", _warnCount);
451 info.AddValue("_infoCount", _infoCount);
452 base.OnGetState(info);
453 }
454
462 protected override void OnSetState(SerializationInfo info)
463 {
464 _errorCount = info.GetValue<int>("_errorCount");
465 _warnCount = info.GetValue<int>("_warnCount");
466 _infoCount = info.GetValue<int>("_infoCount");
467 base.OnSetState(info);
468 }
469 }
470}
bool IsReadOnly
Gets or sets a value indicating whether the list is readonly.
A strongly-typed resource class, for looking up localized strings, etc.
static string RuleMessageRequired
Looks up a localized string similar to Message for broken rule is required.
Stores details about a specific broken business rule.
Definition: BrokenRule.cs:19
string Description
Provides access to the description of the broken rule.
Definition: BrokenRule.cs:55
RuleSeverity Severity
Gets the severity of the broken rule.
Definition: BrokenRule.cs:77
A collection of currently broken rules.
int InformationCount
Gets the number of broken rules in the collection that have a severity of Information.
BrokenRule GetFirstMessage(Csla.Core.IPropertyInfo property, RuleSeverity severity)
Returns the first BrokenRule object corresponding to the specified property and severity.
BrokenRule GetFirstBrokenRule(Csla.Core.IPropertyInfo property)
Returns the first BrokenRule object corresponding to the specified property.
string[] ToArray(RuleSeverity severity)
Returns a string array containing all broken rule descriptions.
string ToString(string separator)
Returns the text of all broken rule descriptions.
int ErrorCount
Gets the number of broken rules in the collection that have a severity of Error.
override void OnSetState(SerializationInfo info)
Override this method to retrieve your field values from the MobileFormatter serialzation stream.
BrokenRule GetFirstBrokenRule(string property)
Returns the first BrokenRule object corresponding to the specified property.
string ToString(RuleSeverity severity)
Returns the text of all broken rule descriptions for a specific severity, each separated by a Environ...
void AddRange(List< BrokenRule > list)
Merges a list of items into the collection.
string ToString(string separator, RuleSeverity severity)
Returns the text of all broken rule descriptions for a specific severity.
int WarningCount
Gets the number of broken rules in the collection that have a severity of Warning.
BrokenRule GetFirstMessage(Csla.Core.IPropertyInfo property)
Returns the first BrokenRule object corresponding to the specified property.
override string ToString()
Returns the text of all broken rule descriptions, each separated by a Environment....
BrokenRule GetFirstMessage(string property, RuleSeverity severity)
Returns the first BrokenRule object corresponding to the specified property and severity.
override void OnGetState(SerializationInfo info)
Override this method to insert your field values into the MobileFormatter serialzation stream.
BrokenRulesCollection()
Creates a read-write instance of the collection.
string[] ToArray()
Returns a string array containing all broken rule descriptions.
string ToString(string separator, RuleSeverity severity, string propertyName)
Returns the text of all broken rule descriptions for a specific severity and property.
Object containing the serialization data for a specific object.
void AddValue(string name, object value)
Adds a value to the serialization stream.
Maintains metadata about a property.
RuleSeverity
Values for validation rule severities.
Definition: RuleSeverity.cs:16
@ Serializable
Prevents updating or inserting until the transaction is complete.