Vérification

Description

Dans ce tutoriel, nous allons apprendre à valider et vérifier les données de nos différents objets.

Solution de départ

La solution de départ est disponible ici

Démonstration

http://tjtechno.com/demo/verification/

Code source

À ajouter dans le fichier Employee.cs

 
        #region Verification
 
        #region GetVerifiers Method
        /// <summary>Get Verifiers.</summary>
        /// <param name="pVerifierProviderContext">Context in which these Verifiers are retrieved.</param>
        /// <returns>The verifiers.</returns>
        [VerifierProvider]
        public static IEnumerable<Verifier> GetVerifiers(Object pVerifierProviderContext) {
 
            List<Verifier> verifiers = new List<Verifier>();
 
            verifiers.Add(GetHireDateRangeVerifier());
            verifiers.Add(new BirthDateRangeVerifier());
            verifiers.Add(GetBornBeforeHiredVerifier());
            verifiers.Add(GetPhoneNumberVerifier(Employee.HomePhoneEntityProperty));
 
            return verifiers;
        }
 
        #endregion
 
        #region Hire Date Verifier
        /// <summary>Get a GetHireDateRangeVerifier.</summary>
        /// <remarks>
        /// Demonstrates building a highly focused verifier
        /// by encapsulation a standard verifier
        /// and its configuration.
        /// </remarks>
        private static Verifier GetHireDateRangeVerifier() {
            Verifier v = new DateTimeRangeVerifier(
                typeof(Employee),       // Type of the object being verified
                Employee.HireDateEntityProperty.Name, // Property trigger
                false,                  // Non-null value is not required
                MinHireDate, true,      // starting min date (inclusive)
                MaxHireDate, false);    // ending max date (exclusive)
            return v;
        }
 
        private static DateTime MinHireDate {
            get {
                return new DateTime(1990, 1, 1);
            }
        }
        private static DateTime MaxHireDate {
            get {
                return DateTime.Today.AddMonths(1);
            }
        }
 
        #endregion
 
        #region BirthDateRangeVerifier inner class
 
        /// <summary>Get the minimum BirthDate allowed.</summary>
        private static DateTime MinBirthDate { get { return new DateTime(1900, 1, 1); } }
 
        /// <summary>BirthDate Range Verifier</summary>
        /// <remarks>
        /// Illustrates changing the error messaging for a particular property.
        /// Have to subclass to take control of the messaging.
        /// Here the message is statically known so we override
        /// <see cref="M:Description"/>;
        /// if it were dynamic or if
        /// <see cref="T:DateTimeRangeVerifier"/> constructed the
        /// message dynamically, we would have overridden
        /// <see cref="M:VerifyValue"/> and manipulated the
        /// message while creating the <see cref="T:VerifierResult"/>.
        /// </remarks>
        private class BirthDateRangeVerifier : DateTimeRangeVerifier {
            /// <summary>Default Ctor,</summary>
            /// <remarks>
            /// BirthDate is not required,
            /// must be on or after global min date (<see cref="M:MinBirthDate"/>), 
            /// and before today.
            /// </remarks>
            public BirthDateRangeVerifier() : base(
                typeof(Employee),             // Type of the object being verified
                Employee.BirthDateEntityProperty.Name, // Property trigger
                false,                        // Non-null value is not required
                MinBirthDate, true,           // starting min date (inclusive)
                DateTime.Today, false) { }    // ending max date (exclusive)
 
            public override string Description {
                // ToDo: Localize
                get {
                    return "Must be born after " + MinBirthDate.Year.ToString() +
                      "; No time travellers allowed!";
                }
            }
        }
        #endregion
 
        #region Born Before Hired Verifier
 
        /// <summary>Get a BornBeforeHiredVerifier.</summary>
        /// <remarks>
        /// Demonstrates comparing two property values
        /// by creating an instance of a 
        /// <see cref="T:DelegateVerifier{TVerifiedObject}"/>.
        /// </remarks>
        private static Verifier GetBornBeforeHiredVerifier() {
            // ToDo: localize description
            string description = "Must be born before hired.";
            DelegateVerifier<Employee> v =
              new DelegateVerifier<Employee>(description, BornBeforeHiredCondition);
 
            v.AddTriggers(Employee.BirthDateEntityProperty.Name, 
                Employee.HireDateEntityProperty.Name);
 
            v.ExecutionModes = VerifierExecutionModes.InstanceAndOnPostsetTriggers;
            return v;
        }
 
        /// <summary>
        /// The <see cref="T:VerifierDelegate{TVerifiedObject}"/>
        /// for the <see cref="M:GetBornBeforeHiredVerifier"/>.
        /// </summary>
        private static VerifierResult BornBeforeHiredCondition(
          Employee pEmp, TriggerContext pTriggerContext, VerifierContext pVerifierContext) {
 
            if (pTriggerContext != null &&
                // We are not checking the proposed value because don't expect to call it preset
              pTriggerContext.Timing == TriggerTiming.Preset) {
                throw new VerifierException("BornBeforeHired verifier not implemented for Preset");
            }
            return new VerifierResult(pEmp.BirthDate < pEmp.HireDate);
        }
 
        #endregion
 
        #region Phone Number Verifier
        /// <summary>Get a GetPhoneNumberVerifier.</summary>
        /// <remarks>
        /// Encapsulates a standard RegexVerifier, subclassed so the description can be customized.
        /// </remarks>
        private static Verifier GetPhoneNumberVerifier(EntityProperty pPhoneEntityProperty) {
            return new PhoneNumberVerifier(
                pPhoneEntityProperty.EntityType, // Type of object being verified
                pPhoneEntityProperty.Name,       // Trigger
                false,                           // Non-null value is not required
                NamedRegexPattern.USPhone);      // Regex pattern to use
        }
 
        private class PhoneNumberVerifier : RegexVerifier {
            public PhoneNumberVerifier(Type pApplicableType, string pPropertyName,
                bool IsRequired, NamedRegexPattern pattern)
                : base(
                    pApplicableType,
                    pPropertyName,
                    IsRequired,
                    pattern
                    ) { }
 
            public override string Description {
                get {
                    return base.Description +
                        " including area code [e.g., (206)555-1212, 206-555-1212, or 206.555.1212].";
                }
            }
        }
 
        #endregion
 

Solution finale

Disponible ici

Liens

Regex en DotNet