Using Self Validation

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.

Self-validation allows you to implement validation logic within the class you want to validate. Use the HasSelfValidation attribute to mark the class that contains the validation logic. Use the SelfValidation attribute to mark each self-validation method within that class. The following code example shows how to do this.

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

[HasSelfValidation]
public class TemperatureRange
{
  private int min;
  private int max;

  // ...

  [SelfValidation]
  public void CheckTemperature(ValidationResults results)
  {
    if (max < min)
      results.AddResult(new ValidationResult("Max less than min", this, "", "", null));
  }
}
'Usage
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators

<HasSelfValidation()> _
Public Class TemperatureRange
  Private min As Integer
  Private max As Integer

  '...

  <SelfValidation()> _
  Sub CheckTemperature(ByVal results As ValidationResults)
    If max < min Then
      results.AddResult(New ValidationResult("Max less than min", Me, "", "", Nothing))
    End If
  End Sub

End Class

In this example, the CheckTemperature method provides self validation. When the Validation.Validate method is called on an instance of TemperatureRange, the CheckTemperature method is invoked.

Each self-validation method must have a void return value and take a ValidationResults instance as its only parameter. The self-validation method should update the ValidationResults instance after performing the validation if the validation fails. For more information about the ValidationResults class, see Understanding Validation Results.

If you have a derived class and you want it to inherit the self-validation behavior of its base class, you must mark both the base class and the derived class with the HasSelfValidation attribute. The self-validation methods in the base class must be public in order for them to be included in the self validation of the derived class.

The SelfValidation attribute has a property named Ruleset. If set, this property indicates that the validation described by the attributed method is part of a named rule set. This allows you to have several SelfValidation methods that each support a different rule set.

Self-validation works in combination with any validators that are assigned to a class. Therefore, the ValidationResults for an object instance will include both the results from the self validation as well as the results from validators within the class. In the following code example, the Address class uses self-validation, and the string ZipCode has the StringLengthValidator assigned.

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

[HasSelfValidation]
public class Address
{
  private string _zipCode;

  [StringLengthValidator(1,10, MessageTemplate="ZipCode Invalid Length")]
  public string ZipCode
  {
    get { return _zipCode; }
    set { _zipCode = value; }
  }

  [SelfValidation]
  public void DoValidate(ValidationResults results)
  {
    ValidationResult result = new ValidationResult("Error Message", typeof(Address), "", "", null);
    ValidationResult result2 = new ValidationResult("Error Message2", typeof(Address), "", "", null);
    results.AddResult(result);
    results.AddResult(result2);
  }
}
'Usage
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Validation
Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators

<HasSelfValidation()> _
Public Class Address

  Private _zipCode As String

  <StringLengthValidator(1, 10, MessageTemplate:="ZipCode Invalid Length")> _
  Public Property ZipCode() As String
    Get
      Return _zipCode
    End Get
    Set(ByVal value As String)
      _zipCode = value
    End Set
  End Property

  <SelfValidation()> _
  Sub DoValidate(ByVal results As ValidationResults)
    Dim result As New ValidationResult("Error Message", GetType(Address), "", "", Nothing)
    Dim result2 As New ValidationResult("Error Message2", GetType(Address), "", "", Nothing)
    results.AddResult(result)
    results.AddResult(result2)
  End Sub

End Class