Click or drag to resize

ValidationService Class

A class that facilitates value validation using IValidator implementations.
Inheritance Hierarchy
SystemObject
  GSF.ValidationValidationService

Namespace: GSF.Validation
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.207-beta+1781b796b2aa7a54013a031eb432fe4ccee31867
Syntax
public class ValidationService
View Source

The ValidationService type exposes the following members.

Constructors
 NameDescription
Public methodValidationService Initializes a new instance of the ValidationService class.
Top
Methods
 NameDescription
Public methodAddValidation Adds a new validation.
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Public methodToStringReturns a string that represents the current object.
(Inherited from Object)
Public methodValidate Executes all validations.
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Example
This example shows how to use ValidationService for input validation:
C#
using System;
using System.Collections.Generic;
using GSF.Validation;

class Program
{
    static void Main(string[] args)
    {
        // Dictionary where captured user input is saved.
        Dictionary<string, string> input = new Dictionary<string, string>();

        // Validation service that will validate user input.
        ValidationService validation = new ValidationService(delegate(string source)
            {
                string value;
                if (input.TryGetValue(source, out value))
                    return input[source];
                else
                    return string.Empty;
            });

        // Add validation rules to the validation service.
        validation.AddValidation("Name", new NonNullStringValidator());
        validation.AddValidation("Email", new EmailAddressValidator());

        // Capture user input.
        Console.Write("Enter name: ");
        input["Name"] = Console.ReadLine();
        Console.Write("Enter email: ");
        input["Email"] = Console.ReadLine();
        Console.WriteLine("");

        // Validate user input.
        string errors;
        if (!validation.Validate(out errors))
            Console.WriteLine(errors);
        else
            Console.WriteLine("No validation errors were found!");

        Console.ReadLine();
    }
}
See Also