PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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//===================================================================================
 17using System;
 18using System.Collections.Generic;
 19using System.Linq;
 20
 21namespace MVVM.Questionnaires.Framework
 22{
 23    /// <summary>
 24    /// Manages validation errors for an object, notifying when the error state changes.
 25    /// </summary>
 26    /// <typeparam name="T"></typeparam>
 27    public class ErrorsContainer<T>
 28    {
 29        private static readonly T[] noErrors = new T[0];
 30        private readonly Action<string> raiseErrorsChanged;
 31        private readonly Dictionary<string, List<T>> validationResults;
 32
 33        /// <summary>
 34        /// Initializes a new instance of the <see cref="ErrorsContainer{T}"/> class.
 35        /// </summary>
 36        /// <param name="raiseErrorsChanged">The action that raises the <see cref="INotifyDataErrorInfo.ErrorsChanged"/>
 37        /// event.</param>
 38        public ErrorsContainer(Action<string> raiseErrorsChanged)
 39        {
 40            if (raiseErrorsChanged == null)
 41            {
 42                throw new ArgumentNullException("raiseErrorsChanged");
 43            }
 44
 45            this.raiseErrorsChanged = raiseErrorsChanged;
 46            this.validationResults = new Dictionary<string, List<T>>();
 47        }
 48
 49        /// <summary>
 50        /// Gets a value that indicates whether the object has validation errors. 
 51        /// </summary>
 52        public bool HasErrors
 53        {
 54            get
 55            {
 56                return this.validationResults.Count != 0;
 57            }
 58        }
 59
 60        /// <summary>
 61        /// Gets the validation errors for a specified property.
 62        /// </summary>
 63        /// <param name="propertyName">The name of the property.</param>
 64        /// <returns>The validation errors of type <typeparamref name="T"/> for the property.</returns>
 65        public IEnumerable<T> GetErrors(string propertyName)
 66        {
 67            var localPropertyName = propertyName ?? string.Empty;
 68            List<T> currentValidationResults = null;
 69            if (this.validationResults.TryGetValue(localPropertyName, out currentValidationResults))
 70            {
 71                return currentValidationResults;
 72            }
 73            else
 74            {
 75                return noErrors;
 76            }
 77        }
 78
 79        /// <summary>
 80        /// Sets the validation errors for the specified property.
 81        /// </summary>
 82        /// <remarks>
 83        /// If a change is detected then the errors changed event is raised.
 84        /// </remarks>
 85        /// <param name="propertyName">The name of the property.</param>
 86        /// <param name="newValidationResults">The new validation errors.</param>
 87        public void SetErrors(string propertyName, IEnumerable<T> newValidationResults)
 88        {
 89            var localPropertyName = propertyName ?? string.Empty;
 90            var hasCurrentValidationResults = this.validationResults.ContainsKey(localPropertyName);
 91            var hasNewValidationResults = newValidationResults != null && newValidationResults.Count() > 0;
 92
 93            if (hasCurrentValidationResults || hasNewValidationResults)
 94            {
 95                if (hasNewValidationResults)
 96                {
 97                    this.validationResults[localPropertyName] = new List<T>(newValidationResults);
 98                    this.raiseErrorsChanged(localPropertyName);
 99                }
100                else
101                {
102                    this.validationResults.Remove(localPropertyName);
103                    this.raiseErrorsChanged(localPropertyName);
104                }
105            }
106        }
107    }
108}