Click here to Skip to main content
15,881,172 members
Articles / Web Development / HTML

Transformalizing NorthWind

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
24 Jul 2014GPL37 min read 57.5K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
//===============================================================================
// Microsoft patterns & practices Enterprise Library
// Validation Application Block
//===============================================================================
// Copyright © 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.
//===============================================================================

using System;
using System.Collections;
using System.Collections.Generic;

namespace Transformalize.Libs.EnterpriseLibrary.Validation
{
	/// <summary>
	/// Represents the result of validating an object.
	/// </summary>
	[Serializable]
	public class ValidationResults : IEnumerable<ValidationResult>
	{
		private List<ValidationResult> validationResults;

		/// <summary>
		/// <para>Initializes a new instance of the <see cref="ValidationResults"/> class with the section name.</para>
		/// </summary>
		public ValidationResults()
		{
			validationResults = new List<ValidationResult>();
		}

		/// <summary>
		/// <para>Adds a <see cref="ValidationResult"/>.</para>
		/// </summary>
		/// <param name="validationResult">The validation result to add.</param>
		public void AddResult(ValidationResult validationResult)
		{
			this.validationResults.Add(validationResult);
		}

		/// <summary>
		/// <para>Adds all the <see cref="ValidationResult"/> instances from <paramref name="sourceValidationResults"/>.</para>
		/// </summary>
		/// <param name="sourceValidationResults">The source for validation results to add.</param>
		public void AddAllResults(IEnumerable<ValidationResult> sourceValidationResults)
		{
			this.validationResults.AddRange(sourceValidationResults);
		}

		/// <summary>
		/// Returns a new instance of <see cref="ValidationResults"/> that includes the results from the receiver that
		/// match the provided tag names.
		/// </summary>
		/// <param name="tagFilter">The indication of whether to include or ignore the matching results.</param>
		/// <param name="tags">The list of tag names to match.</param>
		/// <returns>A <see cref="ValidationResults"/> containing the filtered results.</returns>
		public ValidationResults FindAll(TagFilter tagFilter, params string[] tags)
		{
			// workaround for params behavior - a single null parameter will be interpreted 
			// as null array, not as an array with null as element
			if (tags == null)
			{
				tags = new string[] { null };
			}

			ValidationResults filteredValidationResults = new ValidationResults();

			foreach (ValidationResult validationResult in this)
			{
				bool matches = false;

				foreach (string tag in tags)
				{
					if ((tag == null && validationResult.Tag == null)
						|| (tag != null && tag.Equals(validationResult.Tag)))
					{
						matches = true;
						break;
					}
				}

				// if ignore, look for !match
				// if include, look for match
				if (matches ^ (tagFilter == TagFilter.Ignore))
				{
					filteredValidationResults.AddResult(validationResult);
				}
			}

			return filteredValidationResults;
		}

		/// <summary>
		/// Gets the indication of whether the validation represented by the receiver was successful.
		/// </summary>
		/// <remarks>
		/// An unsuccessful validation will be represented by a <see cref="ValidationResult"/> instance with
		/// <see cref="ValidationResult"/> elements, regardless of these elements' tags.
		/// </remarks>
		public bool IsValid
		{
			get { return validationResults.Count == 0; }
		}

		/// <summary>
		/// Gets the count of results.
		/// </summary>
		public int Count
		{
			get { return this.validationResults.Count; }
		}

		IEnumerator<ValidationResult> IEnumerable<ValidationResult>.GetEnumerator()
		{
			return validationResults.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return validationResults.GetEnumerator();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions