Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:39:27+00:00 2026-05-14T02:39:27+00:00

I’m looking for any recommended validation frameworks, or patterns for an N-tier client application,

  • 0

I’m looking for any recommended validation frameworks, or patterns for an N-tier client application, I want to write the validation method once and bind it to a WPF GUI, and if possible also for server side and client side related business logic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:39:27+00:00Added an answer on May 14, 2026 at 2:39 am

    Very simple, your business entities should include a section to store Business Validation (that is business logic). This will then work whether you are designing a web based app, a client based app, a mobile app, etc. Very flexible.

    Suppose you have a business object (an entity) like a customer:

    public class Customer
     {
             public Customer(int customerId, string company, string city, string country)
            {
                CustomerId = customerId;
                Company = company;
                City = city;
                Country = country;
            }
      }
    

    You then realize that you want your business logic to state that you validate a customerID (in addition it is > 0), you require a company name (without one the customer is not valid), etc etc. These validations are your business logic layer. So you can change your customer class to inherit from say a BusinessObject layer, so your class becomes this:

        public class Customer : BusinessObject
        {
            /// <summary>
            /// Default constructor for customer class.
            /// Initializes automatic properties.
            /// </summary>
            public Customer()
            {
                // Default property values
                Orders = new List<Order>();
    
                // Business rules
                AddRule(new ValidateId("CustomerId"));
    
                AddRule(new ValidateRequired("Company"));
                AddRule(new ValidateLength("Company", 1, 40));
    
            }
    
            /// <summary>
            /// Overloaded constructor for the Customer class.
            /// </summary>
            /// <param name="customerId">Unique Identifier for the Customer.</param>
            /// <param name="company">Name of the Customer.</param>
            /// <param name="city">City where Customer is located.</param>
            /// <param name="country">Country where Customer is located.</param>
            public Customer(int customerId, string company, string city, string country)
                : this()
            {
                CustomerId = customerId;
                Company = company;
                City = city;
                Country = country;
            }
    //much more code like methods...
    }
    

    You do not want objects of BusinessObject type but what businessobject serves as is an abstract class that stores validation errors and business rules. A business rule is ultimately the rules you stated when you called AddRule:

    public abstract class BusinessObject
    {
        /// <summary>
        /// Default value for version number (used in LINQ's optimistic concurrency)
        /// </summary>
        protected static readonly string _versionDefault = "NotSet";
    
        // List of business rules
        private IList<BusinessRule> _businessRules = new List<BusinessRule>();
    
        // List of validation errors (following validation failure)
        private IList<string> _validationErrors = new List<string>();
    
        /// <summary>
        /// Gets list of validations errors.
        /// </summary>
        public IList<string> ValidationErrors
        {
            get { return _validationErrors; }
        }
    
        /// <summary>
        /// Adds a business rule to the business object.
        /// </summary>
        /// <param name="rule"></param>
        protected void AddRule(BusinessRule rule)
        {
            _businessRules.Add(rule);
        }
    
        /// <summary>
        /// Determines whether business rules are valid or not.
        /// Creates a list of validation errors when appropriate.
        /// </summary>
        /// <returns></returns>
        public bool Validate()
        {
            bool isValid = true;
    
            _validationErrors.Clear();
    
            foreach (BusinessRule rule in _businessRules)
            {
                if (!rule.Validate(this))
                {
                    isValid = false;
                    _validationErrors.Add(rule.ErrorMessage);
                }
            }
            return isValid;
        }
    }
    

    Each business rule such as validateid, validaterequired, and validatelength need to be implemented:

    public class ValidateId : BusinessRule
    {
        public ValidateId(string propertyName)
            : base(propertyName)
        {
            ErrorMessage = propertyName + " is an invalid identifier";
        }
    
        public ValidateId(string propertyName, string errorMessage)
            : base(propertyName)
        {
            ErrorMessage = errorMessage;
        }
    
        public override bool Validate(BusinessObject businessObject)
        {
            try
            {
                int id = int.Parse(GetPropertyValue(businessObject).ToString());
                return id >= 0;
            }
            catch
            {
                return false;
            }
        }
    
         public class ValidateLength : BusinessRule
            {
                private int _min;
                private int _max;
    
                public ValidateLength(string propertyName, int min, int max)
                    : base(propertyName)
                {
                    _min = min;
                    _max = max;
    
                    ErrorMessage = propertyName + " must be between " + _min + " and " + _max + " characters long.";
                }
    
                public ValidateLength(string propertyName, string errorMessage, int min, int max)
                    : this(propertyName, min, max)
                {
                    ErrorMessage = errorMessage;
                }
    
                public override bool Validate(BusinessObject businessObject)
                {
                    int length = GetPropertyValue(businessObject).ToString().Length;
                    return length >= _min && length <= _max;
                }
            }
    

    Those are just two samples of validatelength, and validateid, you can come up with validaterequired (check if the value exists). They both implement a business rule class:

    public abstract class BusinessRule
        {
            public string PropertyName { get; set; }
            public string ErrorMessage { get; set; }
    
            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="propertyName">The property name to which rule applies.</param>
            public BusinessRule(string propertyName)
            {
                PropertyName = propertyName;
                ErrorMessage = propertyName + " is not valid";
            }
    
            /// <summary>
            /// Overloaded constructor
            /// </summary>
            /// <param name="propertyName">The property name to which rule applies.</param>
            /// <param name="errorMessage">The error message.</param>
            public BusinessRule(string propertyName, string errorMessage)
                : this(propertyName)
            {
                ErrorMessage = errorMessage;
            }
    
            /// <summary>
            /// Validation method. To be implemented in derived classes.
            /// </summary>
            /// <param name="businessObject"></param>
            /// <returns></returns>
            public abstract bool Validate(BusinessObject businessObject);
    
            /// <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(BusinessObject businessObject)
            {
                return businessObject.GetType().GetProperty(PropertyName).GetValue(businessObject, null);
            }
        }
    

    So your businessobject class just keeps a list of required business rules and the validation errors it catches. The business rule class simply stores the property name and an error message in case a rule is not correctly applied. It also contains an abstract validate() method which is defined for each validation class. It differs for each validation class because validating an id is different then validation a required field.

    The gang of four website has a lot of good help regarding this. Much of this code is taken from their models.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I want to construct a data frame in an Rcpp function, but when I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.