Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET

An Object Level Validation Framework

Rate me:
Please Sign up or sign in to vote.
4.94/5 (28 votes)
10 Nov 20065 min read 110.6K   492   85  
Provides a basic/advanced object level framework for validation.
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Cobalt.Validation.Validators
{
    /// <summary>
    /// Performs validation based on a value comparison.
    /// </summary>
    public class CompareValidator : Validator
    {

        #region Private Fields

        private ComparisonOperator comparisonOperator;
        private object valueToCompare;

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the comparison operator.
        /// </summary>
        /// <value>The comparison operator.</value>
        public ComparisonOperator ComparisonOperator
        {
            get { return this.comparisonOperator; }
        }

        /// <summary>
        /// Gets the value to compare.
        /// </summary>
        /// <value>The value to compare.</value>
        public object ValueToCompare
        {
            get { return this.valueToCompare; }
        }

        #endregion

        #region Protected Properties

        /// <summary>
        /// Gets the valid property types.
        /// </summary>
        /// <value>The valid property types.</value>
        protected override Type[] ValidPropertyTypes
        {
            get { return new Type[] { typeof(IComparable) }; }
        }

        #endregion

        #region Constructor(s)

        /// <summary>
        /// Initializes a new instance of the <see cref="CompareValidator"/> class.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        /// <param name="comparisonOperator">The comparison operator.</param>
        /// <param name="valueToCompare">The value to compare.</param>
        public CompareValidator(PropertyInfo propertyInfo, ComparisonOperator comparisonOperator, object valueToCompare)
            : this(null, propertyInfo, comparisonOperator, valueToCompare)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompareValidator"/> class.
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <param name="propertyInfo">The property info.</param>
        /// <param name="comparisonOperator">The comparison operator.</param>
        /// <param name="valueToCompare">The value to compare.</param>
        public CompareValidator(string errorMessage, PropertyInfo propertyInfo, ComparisonOperator comparisonOperator, object valueToCompare)
            : base(errorMessage, propertyInfo)
        {
            this.comparisonOperator = comparisonOperator;
            this.valueToCompare = valueToCompare;
            if (string.IsNullOrEmpty(errorMessage))
                this.ErrorMessage = string.Format(Resources.DefaultErrorMessages.Compare, propertyInfo.Name, this.comparisonOperator.ToString(), this.valueToCompare);
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Does the validation.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        protected override bool DoIsValid(object instance, object value)
        {
            IComparable comp = value as IComparable;

            object valueToCompare = Convert.ChangeType(this.valueToCompare, value.GetType());

            return TestComparisonResult(comp.CompareTo(valueToCompare));
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Tests the comparison result.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        private bool TestComparisonResult(int result)
        {
            switch (this.comparisonOperator)
            {
                case ComparisonOperator.Equal:
                    return result == 0;
                case ComparisonOperator.NotEqual:
                    return result != 0;
                case ComparisonOperator.GreaterThan:
                    return result > 0;
                case ComparisonOperator.GreaterThanOrEqual:
                    return result >= 0;
                case ComparisonOperator.LessThan:
                    return result < 0;
                case ComparisonOperator.LessThanOrEqual:
                    return result <= 0;
            }
            return false;
        }

        #endregion

        #region Public Static Methods

        /// <summary>
        /// Initializes a new instance of the <see cref="CompareValidator"/> class.
        /// </summary>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="comparisonOperator">The comparison operator.</param>
        /// <param name="valueToCompare">The value to compare.</param>
        /// <returns></returns>
        public static CompareValidator CreateValidator<T>(string propertyName, ComparisonOperator comparisonOperator, object valueToCompare)
        {
            return CreateValidator(typeof(T), propertyName, comparisonOperator, valueToCompare);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompareValidator"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="comparisonOperator">The comparison operator.</param>
        /// <param name="valueToCompare">The value to compare.</param>
        /// <returns></returns>
        public static CompareValidator CreateValidator(Type type, string propertyName, ComparisonOperator comparisonOperator, object valueToCompare)
        {
            return new CompareValidator(Validator.GetPropertyInfo(type, propertyName), comparisonOperator, valueToCompare);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompareValidator"/> class.
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="comparisonOperator">The comparison operator.</param>
        /// <param name="valueToCompare">The value to compare.</param>
        /// <returns></returns>
        public static CompareValidator CreateValidator<T>(string errorMessage, string propertyName, ComparisonOperator comparisonOperator, object valueToCompare)
        {
            return CreateValidator(typeof(T), errorMessage, propertyName, comparisonOperator, valueToCompare);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CompareValidator"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="comparisonOperator">The comparison operator.</param>
        /// <param name="valueToCompare">The value to compare.</param>
        /// <returns></returns>
        public static CompareValidator CreateValidator(Type type, string errorMessage, string propertyName, ComparisonOperator comparisonOperator, object valueToCompare)
        {
            return new CompareValidator(errorMessage, Validator.GetPropertyInfo(type, propertyName), comparisonOperator, valueToCompare);
        }

        #endregion

    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I work at a financial services firm using mostly .NET. I enjoy my job immensely and love teaching, training, and mentoring younger developers.

Comments and Discussions