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.
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.
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.
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.