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

  • Home
  • SEARCH
  • 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 596757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:12:23+00:00 2026-05-13T16:12:23+00:00

From my experience many validation frameworks in .NET allow you to validate a single

  • 0

From my experience many validation frameworks in .NET allow you to validate a single field at a time for doing things like ensuring a field is a postal code or email address for instance. I usually call these within-field edits.

In my project we often have to do between-field-edits though. For instance, if you have a class like this:

public class Range
{
    public int Min { get; set; }
    public int Max { get; set; }
}

you might want to ensure that Max is greater than Min. You might also want to do some validation against an external object. For instance given you have a class like this:

public class Person
{
    public string PostalCode { get; set; }
}

and for whatever reason you want to ensure that Postal Code exists in a database or a file provided to you. I have more complex examples like where a user provides a data dictionary and you want to validate your object against that data dictionary.

My question is: can we use any of the existing validation frameworks (TNValidate, NHibernate Validator) for .NET or do we need to use a rules engine or what?? How do you people in the real world deal with this situation? 🙂

  • 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-13T16:12:23+00:00Added an answer on May 13, 2026 at 4:12 pm

    There’s only one validation framework that I know well and that is Enterprise Library Validation Application Block, or VAB for short. I will answer your questions from the context of the VAB.

    First question: Can you do state (between-field) validation in VAB?

    Yes you can. There are multiple ways to do this. You can choose for the self validation mechanism, as follows:

    [HasSelfValidation]
    public class Range
    {
        public int Min { get; set; }
        public int Max { get; set; }
    
        [SelfValidation]
        public void ValidateRange(ValidationResults results)
        {
            if (this.Max < this.Min)
            {
                results.AddResult(
                    new ValidationResult("Max less than min", this, "", "", null));
            }
        }
    }
    

    I must say I personally don’t like this type of validations, especially when validating my domain entities, because I like to keep my validations separate from the validation logic (and keep my domain logic free from references to any validation framework). However, they need considerably less code than the alternative, which is writing a custom validator class. Here’s an example:

    [ConfigurationElementType(typeof(CustomValidatorData))]
    public sealed class RangeValidator : Validator
    {
        public RangeValidator(NameValueCollection attributes)
            : base(string.Empty, string.Empty) { }
    
        protected override string DefaultMessageTemplate
        {
            get { throw new NotImplementedException(); }
        }
    
        protected override void DoValidate(object objectToValidate,
            object currentTarget, string key, ValidationResults results)
        {
            Range range = (Range)currentTarget;
    
            if (range.Max < range.Min)
            {
                this.LogValidationResult(results,
                    "Max less than min", currentTarget, key);
            }
        }
    }
    

    After writing this class you can hook this class up in your validation configuration file like this:

    <validation>
      <type name="Range" defaultRuleset="Default" assemblyName="[Range Assembly]">
        <ruleset name="Default">
          <validator type="[Namespace].RangeValidator, [Validator Assembly]"
            name="Range Validator" />
        </ruleset>
      </type>
    </validation> 
    

    Second question: How to do complex validations with possible interaction a database (with VAB).

    The examples I give for the first question are also usable for this. You can use the same techniques: self validation and custom validator. Your scenario where you want to check a value in a database is actually a simple one, because the validity of your object is not based on its context. You can simply check the state of the object against the database. It gets more complicated when the context in which an object lives gets important (but it is possible with VAB). Imagine for instance that you want to write a validation that ensures that every customer, at a given moment in time, has no more than two unshipped orders. This not only means that you have to check the database, but perhaps new orders that are added or orders are deleted within that same context. This problem is not VAB specific, you will have the same problems with every framework you choose. I’ve written an article that describes the complexities we’re facing with in these situations (read and shiver).

    Third question: How do you people in the real world deal with this situation?

    I do these types of validation with the VAB in production code. It works great, but VAB is not very easy to learn. Still, I love what we can do with VAB, and it will only get better when v5.0 comes out. When you want to learn it, start with reading the ValidationHOL.pdf document that you can found in the Hands-On Labs download.

    I hope this helps.

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

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer i wrote a class class DbPerformance { var $TablesHaveOverHead =… May 15, 2026 at 6:43 am
  • Editorial Team
    Editorial Team added an answer As stackoverflow is for programming issues, You’ll probably get a… May 15, 2026 at 6:43 am
  • Editorial Team
    Editorial Team added an answer You are almost there! The following two marks should do… May 15, 2026 at 6:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.