Woah, Nellie.NET!

From my simple mind to… mine…

Validation Application Block – a need for behavior driven attributes?

with one comment

I’ve been following the Validation Application Block since introduced to me by Agile Joe a few months ago.

I’ve also been reading up on Behavior Driven Design. I love this stuff. The contextual clarity provided by BDD should really take agile development to the next level.

So why can’t VAB take a page out of the BDD book?

Have a look at this code, utilizing VAB (borrowed from this article):

public class Subscriber
{
private string _name;
[NotNullValidator(MessageTemplate
= "Name cannot be null.")]
[StringLengthValidator(
1,100,MessageTemplate = "Max 100 characters.")]
public string Name
{
get { return _name; }
set { _name = value; }
}

private string _emailAddress;
[NotNullValidator(MessageTemplate
= "EmailAddress cannot be null.")]
[StringLengthValidator(
1, 100, MessageTemplate = "Max 100 characters.")]
[RegexValidator(
@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate = "Invalid.")]
public string EmailAddress
{
get { return _emailAddress; }
set { _emailAddress = value; }
}

public Subscriber() {}
}

Have a look at the validation attributes:

[NotNullValidator(MessageTemplate = "EmailAddress cannot be null.")]
[StringLengthValidator(
1, 100, MessageTemplate = "Max 100 characters.")]
[RegexValidator(
@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate = "Invalid.")]

Pretty cool right? You prepend your properties with this attributes and VAB takes care of the rest of the work!

Well, yeah, that is cool. But from a BDD point of view, wouldn’t it be cool if there was some way to more clearly say, “The EmailAddress should exist, with a length between 1 and 100, and be properly formatted”:

[EmailAddressMustExist]
[EmailAddressShouldMeetLengthRequirements
]
[EmailAddressShouldBeProperlyFormatted
]

Having these attributes defined elsewhere (seperation of concerns), of course.

Now, you have a more clearly defined domain. You have continued to leave the validation of these rules elsewhere, but the clarity of these rules is significantly higher simply because the rules READ well.

I know, I know… we’re all programmers. I should be able to read a regex in 2.3 nanoseconds. Of course,

I don’t know VAB well enough to know if it already allows this behavior in its attributes. Let me know if it does. 🙂

Advertisements

Written by Nelson

February 18, 2007 at 1:49 pm

One Response

Subscribe to comments with RSS.

  1. Excellent post Nellie!

    I agree with your approach on utilizing BDD within the VAB. You should be able to inherit or extend the attributes that are within the VAB and override the validation method with what you would traditionally use within the decorator. Can you say reuse!

    Joe

    February 20, 2007 at 2:52 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: