/V4/MVVM RI/MVVM.Questionnaires/Framework/ErrorsContainer.cs

# · C# · 108 lines · 60 code · 7 blank · 41 comment · 9 complexity · 50a44ecc516deb0f92b3fbb0bb9e6416 MD5 · raw file

  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. namespace MVVM.Questionnaires.Framework
  21. {
  22. /// <summary>
  23. /// Manages validation errors for an object, notifying when the error state changes.
  24. /// </summary>
  25. /// <typeparam name="T"></typeparam>
  26. public class ErrorsContainer<T>
  27. {
  28. private static readonly T[] noErrors = new T[0];
  29. private readonly Action<string> raiseErrorsChanged;
  30. private readonly Dictionary<string, List<T>> validationResults;
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref="ErrorsContainer{T}"/> class.
  33. /// </summary>
  34. /// <param name="raiseErrorsChanged">The action that raises the <see cref="INotifyDataErrorInfo.ErrorsChanged"/>
  35. /// event.</param>
  36. public ErrorsContainer(Action<string> raiseErrorsChanged)
  37. {
  38. if (raiseErrorsChanged == null)
  39. {
  40. throw new ArgumentNullException("raiseErrorsChanged");
  41. }
  42. this.raiseErrorsChanged = raiseErrorsChanged;
  43. this.validationResults = new Dictionary<string, List<T>>();
  44. }
  45. /// <summary>
  46. /// Gets a value that indicates whether the object has validation errors.
  47. /// </summary>
  48. public bool HasErrors
  49. {
  50. get
  51. {
  52. return this.validationResults.Count != 0;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets the validation errors for a specified property.
  57. /// </summary>
  58. /// <param name="propertyName">The name of the property.</param>
  59. /// <returns>The validation errors of type <typeparamref name="T"/> for the property.</returns>
  60. public IEnumerable<T> GetErrors(string propertyName)
  61. {
  62. var localPropertyName = propertyName ?? string.Empty;
  63. List<T> currentValidationResults = null;
  64. if (this.validationResults.TryGetValue(localPropertyName, out currentValidationResults))
  65. {
  66. return currentValidationResults;
  67. }
  68. else
  69. {
  70. return noErrors;
  71. }
  72. }
  73. /// <summary>
  74. /// Sets the validation errors for the specified property.
  75. /// </summary>
  76. /// <remarks>
  77. /// If a change is detected then the errors changed event is raised.
  78. /// </remarks>
  79. /// <param name="propertyName">The name of the property.</param>
  80. /// <param name="newValidationResults">The new validation errors.</param>
  81. public void SetErrors(string propertyName, IEnumerable<T> newValidationResults)
  82. {
  83. var localPropertyName = propertyName ?? string.Empty;
  84. var hasCurrentValidationResults = this.validationResults.ContainsKey(localPropertyName);
  85. var hasNewValidationResults = newValidationResults != null && newValidationResults.Count() > 0;
  86. if (hasCurrentValidationResults || hasNewValidationResults)
  87. {
  88. if (hasNewValidationResults)
  89. {
  90. this.validationResults[localPropertyName] = new List<T>(newValidationResults);
  91. this.raiseErrorsChanged(localPropertyName);
  92. }
  93. else
  94. {
  95. this.validationResults.Remove(localPropertyName);
  96. this.raiseErrorsChanged(localPropertyName);
  97. }
  98. }
  99. }
  100. }
  101. }