Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / C#

Test Driving NHibernate 3.0, LINQ and the Entity Framework CTP 5 with the Abstract Factory Design Pattern

Rate me:
Please Sign up or sign in to vote.
4.89/5 (18 votes)
2 Apr 2011CPOL12 min read 229.4K   1.7K   53  
Developing an N-Tier application with C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ORMApplicationServices
{
   
    /// <summary>
    /// Validation Rules
    /// </summary>
    public class ValidationRules
    {
        private Object _businessObject;
        private Boolean _returnStatus { get; set; }
        private List<String> _returnMessage { get; set; }
        private Hashtable _validationErrors;

        public Boolean ReturnStatus { get { return _returnStatus; }  }
        public List<String> ReturnMessage { get { return _returnMessage; } }
        public Hashtable ValidationErrors { get { return _validationErrors; } }
        public Object BusinessObject { set { _businessObject = value; } }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="businessObject"></param>
        public ValidationRules(Object businessObject)
        {
            _businessObject = businessObject;
            _returnStatus = true;
            _returnMessage = new List<string>();
            _validationErrors = new Hashtable();
        }

        /// <summary>
        /// Validate Required
        /// </summary>
        /// <param name="propertyName"></param>
        public Boolean ValidateRequired(string propertyName)
        {
            return ValidateRequired(propertyName, propertyName);
        }

        /// <summary>
        /// Validate Required
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="friendlyName"></param>
        public Boolean ValidateRequired(string propertyName, string friendlyName)
        {
            object valueOf = GetPropertyValue(propertyName);        
            if (ORMUtilities.Validations.ValidateRequired(valueOf) == false)
            {
                string errorMessage = friendlyName + " is a required field.";
                AddValidationError(propertyName, errorMessage);                
                return false;
            }

            return true; 

        }

        /// <summary>
        /// Validate Length
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="maxLength"></param>
        public Boolean ValidateLength(string propertyName, int maxLength)
        {
            return ValidateLength(propertyName, propertyName, maxLength);
        }

        /// <summary>
        /// Validate Length
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="maxLength"></param>
        public Boolean ValidateLength(string propertyName, string friendlyName, int maxLength)
        {
            object valueOf = GetPropertyValue(propertyName);                     
            if (ORMUtilities.Validations.ValidateLength(valueOf, maxLength) == false)
            {
                string errorMessage = friendlyName + " exceeds the maximum of " + maxLength + " characters long.";
                AddValidationError(propertyName, errorMessage);                     
                return false;
            }

            return true;
        }

        /// <summary>
        /// Validate Email Address
        /// </summary>
        /// <param name="propertyName"></param>
        public Boolean ValidateEmailAddress(string propertyName)
        {
            return ValidateEmailAddress(propertyName, propertyName);
        }
        /// <summary>
        /// Validate Email Address
        /// </summary>
        /// <param name="propertyName"></param>
        public Boolean ValidateEmailAddress(string propertyName, string friendlyName)
        {
            object valueOf = GetPropertyValue(propertyName);

            if (valueOf == null) return true;          

            if (ORMUtilities.Validations.ValidateEmailAddress(valueOf.ToString()) == false)
            {
                string emailAddressErrorMessage = friendlyName + " is not a valid email address";
                AddValidationError(propertyName, emailAddressErrorMessage);            
                return false;
            }

            return true;
        }

        /// <summary>
        /// Gets value for given business object's property using reflection.
        /// </summary>
        /// <param name="businessObject"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        protected object GetPropertyValue(string propertyName)
        {
            return _businessObject.GetType().GetProperty(propertyName).GetValue(_businessObject, null);
        }      

        /// <summary>
        /// Add Validation Error
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="friendlyName"></param>
        /// <param name="errorMessage"></param>
        public void AddValidationError(string propertyName, string errorMessage)
        {
            _returnMessage.Add(errorMessage);
            
            if (_validationErrors.Contains(propertyName) == false)
                _validationErrors.Add(propertyName, errorMessage);

            _returnStatus = false;
        }

    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer Joey Software Solutions
United States United States
Mark Caplin has specialized in Information Technology solutions for the past 30 years. Specializing in full life-cycle development projects for both enterprise-wide systems and Internet/Intranet based solutions.

For the past fifteen years, Mark has specialized in the Microsoft .NET framework using C# as his tool of choice. For the past four years Mark has been implementing Single Page Applications using the Angular platform.

When not coding, Mark enjoys playing tennis, listening to U2 music, watching Miami Dolphins football and watching movies in Blu-Ray technology.

In between all this, his wife of over 25 years, feeds him well with some great home cooked meals.

You can contact Mark at mark.caplin@gmail.com

...

Comments and Discussions