Up Validate
Documentation.Glossary » Validate » Discover verifiers

Discover verifiers

Last modified on August 15, 2012 17:20

A VerifierEngine is both a container and an execution engine for verifiers. Verifier instances are added to each engine either via a discovery process or programmatically.


Verifier discovery

The VerifierEngine lazily discovers verifiers in the types it is asked to verify. Note: automatic discovery is not always a good thing, and developers can disable an engine’s automatic discovery. An engine with automatic discovery disabled can still perform discovery when asked to do so. 

When a VerifierEngine attempts to verify an instance of a type it has not seen before, it probes the type reflectively, looking for verifiers defined for the type and its base types.

The probing strategy is as follows.

  1. Look for instances of the VerifierAttribute. DevForce provides a number of common verifiers in attribute form all of which descend from VerifierAttribute. The developer can also add custom VerifierAttribute subclasses just as he can add custom Verifiers.  These define the “attributed verifiers”. In Silverlight applications, .NET ValidationAttributes will also be discovered.
  2. Look for VerifierAttributes on the members of the metadata "buddy" class defined for the type
  3. Look for any implementations of the IVerifierProvider interface, and call the GetVerifiers method of each. 

We have seen the attribute verifiers earlier.

A VerifierProvider might look like the following:

C#
/// <summary>
/// Implement IVerifierProvider interface to give DevForce
/// the verifiers in use at run time.
/// </summary>
public class VerifierProvider : IVerifierProvider {

public IEnumerable<Verifier> GetVerifiers(
  object verifierProviderContext) {
    List<Verifier> verifiers = new List<Verifier>();
    verifiers.Add(GetHireDateRangeVerifier());
    verifiers.Add(new BirthDateRangeVerifier());
    verifiers.Add(GetBornBeforeHiredVerifier());
    verifiers.Add(GetPhoneNumberVerifier(Employee.PropertyMetadata.HomePhone));
   return verifiers;
  }
}
VB
''' <summary>
''' Implement IVerifierProvider interface to give DevForce
''' the verifiers in use at run time.
''' </summary>
Public Class VerifierProvider
 Implements IVerifierProvider

 Public Function GetVerifiers(ByVal verifierProviderContext _
   As Object) As IEnumerable(Of Verifier)
   Dim verifiers As New List(Of Verifier)()
    verifiers.Add(GetHireDateRangeVerifier())
    verifiers.Add(new BirthDateRangeVerifier())
    verifiers.Add(GetBornBeforeHiredVerifier())
    verifiers.Add(GetPhoneNumberVerifier(Employee.PropertyMetadata.HomePhone))
   Return verifiers
 End Function
End Class

The class that implements the IVerifierProvider must have an empty public constructor.  The reason for this is that DevForce will internally create an instance of this type and call its GetVerifiers method.  For this reason it is usually a good idea to have your IVerifierProvider implementation be a completely separate class, so that you will not be tempted to remove the public constructor for other reasons.

Adding or removing a verifier programmatically

Verifiers can be also added or removed from a VerifierEngine by calling its AddVerifier and RemoveVerifier methods. Verifiers can be added or removed from a VerifierEngine at any time.

The engine raises a VerifiersChanged event whenever verifiers are added or removed. This event will inform any subscriber of the addition or removal of any verifier. Note that the event is also raised when triggers are added or removed from a verifier that has previously been registered in the engine.

Created by DevForce on October 06, 2010 15:13

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade