/V4/MVVM RI/MVVM.Questionnaires/Framework/ErrorsContainer.cs
# · C# · 108 lines · 60 code · 7 blank · 41 comment · 9 complexity · 50a44ecc516deb0f92b3fbb0bb9e6416 MD5 · raw file
- //===================================================================================
- // Microsoft patterns & practices
- // Composite Application Guidance for Windows Presentation Foundation and Silverlight
- //===================================================================================
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
- // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
- // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- // FITNESS FOR A PARTICULAR PURPOSE.
- //===================================================================================
- // The example companies, organizations, products, domain names,
- // e-mail addresses, logos, people, places, and events depicted
- // herein are fictitious. No association with any real company,
- // organization, product, domain name, email address, logo, person,
- // places, or events is intended or should be inferred.
- //===================================================================================
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace MVVM.Questionnaires.Framework
- {
- /// <summary>
- /// Manages validation errors for an object, notifying when the error state changes.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public class ErrorsContainer<T>
- {
- private static readonly T[] noErrors = new T[0];
- private readonly Action<string> raiseErrorsChanged;
- private readonly Dictionary<string, List<T>> validationResults;
-
- /// <summary>
- /// Initializes a new instance of the <see cref="ErrorsContainer{T}"/> class.
- /// </summary>
- /// <param name="raiseErrorsChanged">The action that raises the <see cref="INotifyDataErrorInfo.ErrorsChanged"/>
- /// event.</param>
- public ErrorsContainer(Action<string> raiseErrorsChanged)
- {
- if (raiseErrorsChanged == null)
- {
- throw new ArgumentNullException("raiseErrorsChanged");
- }
-
- this.raiseErrorsChanged = raiseErrorsChanged;
- this.validationResults = new Dictionary<string, List<T>>();
- }
-
- /// <summary>
- /// Gets a value that indicates whether the object has validation errors.
- /// </summary>
- public bool HasErrors
- {
- get
- {
- return this.validationResults.Count != 0;
- }
- }
-
- /// <summary>
- /// Gets the validation errors for a specified property.
- /// </summary>
- /// <param name="propertyName">The name of the property.</param>
- /// <returns>The validation errors of type <typeparamref name="T"/> for the property.</returns>
- public IEnumerable<T> GetErrors(string propertyName)
- {
- var localPropertyName = propertyName ?? string.Empty;
- List<T> currentValidationResults = null;
- if (this.validationResults.TryGetValue(localPropertyName, out currentValidationResults))
- {
- return currentValidationResults;
- }
- else
- {
- return noErrors;
- }
- }
-
- /// <summary>
- /// Sets the validation errors for the specified property.
- /// </summary>
- /// <remarks>
- /// If a change is detected then the errors changed event is raised.
- /// </remarks>
- /// <param name="propertyName">The name of the property.</param>
- /// <param name="newValidationResults">The new validation errors.</param>
- public void SetErrors(string propertyName, IEnumerable<T> newValidationResults)
- {
- var localPropertyName = propertyName ?? string.Empty;
- var hasCurrentValidationResults = this.validationResults.ContainsKey(localPropertyName);
- var hasNewValidationResults = newValidationResults != null && newValidationResults.Count() > 0;
-
- if (hasCurrentValidationResults || hasNewValidationResults)
- {
- if (hasNewValidationResults)
- {
- this.validationResults[localPropertyName] = new List<T>(newValidationResults);
- this.raiseErrorsChanged(localPropertyName);
- }
- else
- {
- this.validationResults.Remove(localPropertyName);
- this.raiseErrorsChanged(localPropertyName);
- }
- }
- }
- }
- }