Share via


Using Self Validation

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.

If you do not specify a rule set name in the SelfValidation attribute, the self validation routine is part of the default rule set. To specify a rule set name, include the Ruleset parameter in the SelfValidation attribute as shown below. You can include more than one SelfValidation method in a class and differentiate them using rule set names.

[SelfValidation(Ruleset="SimpleRuleset")]
public void Validate(ValidationResults results)
{
  ...
}
'Usage
<SelfValidation(Ruleset:="SimpleRuleset")> _
Public Sub Validate(results As ValidationResults)
  ...
End Sub

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.

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