High quality input validation is essential to many, if not all systems that we write. It doesn’t matter whether we’re validating user input in a WPF or Silverlight application, checking data transfer objects passed into a service layer or verifying the information in a JSON packet submitted by REST, validation is essential.

Lately, I’ve been working with a bunch of code that looks like this:

var results = new List<ValidationResult>();
if (string.IsNullOrWhitespace(myPerson.FullName))
{
    results.Add( 
        new ValidationResult(
            "FullName", "Full Name is a mandatory field"));
}
else if (myPerson.FullName.Length > 100)
{
    results.Add( 
        new ValidationResult(
            "FullName", "Full Name must be 100 characters or less."));
}

if (string.IsNullOrWhitespace(myPerson.KnownAs))
{
    results.Add(
        new ValidationResult(
            "KnownAs", "Known As is a mandatory field"));
}
else if (myPerson.KnownAs.Length > 50)
{
    results.Add( 
        new ValidationResult(
            "KnownAs", "Known As must be 50 characters or less."));
}

This code works - but it is ugly. The code is verbose and repetitive - the mechanics of building ValidationResult items and adding them to the results list swamp the actual checks being made.

Inspired in part by an Ivan Towlson presentation at Microsoft TechEd 2011 in New Zealand, I’ve got a prototype going in Visual Studio 2010 that looks like this:

var results = new List<ValidationResult>();
using (dynamic check = new ValidationContext(myPerson, results))
{
    check.FullName( mandatory: true, maxlength: 100);
    check.KnownAs( mandatory: true, maxlength: 50);
}

How on earth does this work?

  • The ValidationContext class provides an ambient context for validation that gathers up validation results in the background, without any need to pass around the list itself.

  • Leveraging the new dynamic keyword in C# 4 allows the ValidationContext to behave as though it has a method for each property declared on myPerson. The named parameters passed to these dynamic methods specify the requested checks.

What do you think? Does this qualify as evil code, or merely expressive?

Should I turn this prototype into an actual library and make it available for download?

Update: Ivan Towlson has responded with a blog entry of his own: Dynamic interfaces in C#

Comments

blog comments powered by Disqus
Next Post
Bindable Run for FlowDocuments  05 Sep 2011
Prior Post
What to ask users ...  15 Aug 2011
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
Archives
August 2011
2011