Using Attributes to Define Validation Rule Sets

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

You can include attributes in your code to define rule sets. This is an alternative to using configuration or code. The following code example shows how to use attributes to define a rule set named "RuleSetA."

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public class Customer
{
    private string firstName;
    private string lastName;
    private DateTime dateOfBirth;
    private string email;
    private Address address;

    [StringLengthValidator(1, 50, Ruleset="RuleSetA",
            MessageTemplate="First Name must be between 1 and 50 characters")]
    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    [StringLengthValidator(1, 50, Ruleset = "RuleSetA", 
    MessageTemplate = "Last Name must be between 1 and 50 characters")]
    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    [RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, 
            DateTimeUnit.Year, Ruleset="RuleSetA", 
            MessageTemplate="Must be 18 years or older.")]
    public DateTime DateOfBirth
    {
        get { return dateOfBirth; }
        set { dateOfBirth = value; }
    }

    [RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", 
            Ruleset = "RuleSetA")]
    public string Email
    {
        get { return email; }
        set { email = value; }
    }

    [ObjectValidator("ValidAddress", Ruleset="RuleSetA")]
    public Address Address
    {
        get { return address; }
        set { address = value; }
    }

}
'Usage
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators
Public Class Customer

    Private _firstName As String
    Private _lastName As String
    Private _dateOfBirth As DateTime
    Private _email As String
    Private _address As Address 
    Private _rewardPoints As Integer

    <StringLengthValidator(1, 50, Ruleset:="RuleSetA", MessageTemplate:="First Name must be between 1 and 50 characters")> _
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal value As String)
            _firstName = value
        End Set
    End Property


    <StringLengthValidator(1, 50, Ruleset:="RuleSetA", MessageTemplate:="Last Name must be between 1 and 50 characters")> _
    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal value As String)
            _lastName = value
        End Set
    End Property

    <RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, Ruleset:="RuleSetA", MessageTemplate:="Must be 18 years or older.")> _
    Public Property DateOfBirth() As DateTime
        Get
            Return _dateOfBirth
        End Get
        Set(ByVal value As DateTime)
            _dateOfBirth = value
        End Set
    End Property

    <RegexValidator("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate:="Invalid e-mail address", Ruleset:="RuleSetA")> _
    Public Property Email() As String
        Get
            Return _email
        End Get
        Set(ByVal value As String)
            _email = value
        End Set
    End Property

    <ObjectValidator("RuleSetA", Ruleset:="RuleSetA")> _
    Public Property Address() As Address
        Get
            Return _address
        End Get
        Set(ByVal value As Address)
            _address = value
        End Set
    End Property

    <RangeValidator(0, RangeBoundaryType.Inclusive, 1000000, RangeBoundaryType.Inclusive, Ruleset:="RuleSetA", MessageTemplate:="Rewards points cannot exceed 1,000,000")> _
    Public Property RewardPoints() As Integer
        Get
            Return _rewardPoints
        End Get
        Set(ByVal value As Integer)
            _rewardPoints = value
        End Set
    End Property

End Class

This code defines a class named Customer that includes a number of properties such as firstName and lastName. Attributes that are attached to these properties associate them with validators. For example, the StringLengthValidator attribute is attached to the firstName property and associates it with the StringLengthValidator class. This attribute includes two constructor parameters that constrain the length of the value contained in the firstName field, a parameter that names the rule set and a parameter that defines the message template. The message template contains the message that is logged if the validation fails.

The Ruleset parameter of the validation attributes indicates that the application block will use "RuleSetA" rather than the anonymous, default rule set. In this example, the ObjectValidator attribute is a part of the "RuleSetA" rule set, and refers to the "RuleSetA" rule set of the Address class.

Note

Remember that C# allows named parameters to appear after positional attribute parameters. The parameters are referenced with the Property=value syntax. The Validation Application Block includes a common set of properties, such as Ruleset, that you can use with every validator attribute in addition to the validator-specific constructor parameters. The ObjectValidator attribute portion of the code example is an example of this.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.