protected override ValidationResult IsValid(object value, ValidationContext validationContext) { try { CustomerFormViewModel customer = (CustomerFormViewModel)validationContext.ObjectInstance; MembershipAgeRequirements ageRequirements = new MembershipAgeRequirements(); BusinessRulesResult rulesResult = ageRequirements.IsCustomerAgeAcceptable(customer.MembershipTypeId, customer.BirthDate); return((rulesResult.IsErrored == true) ? new ValidationResult(rulesResult.ErrorMessage) : ValidationResult.Success); } catch (InvalidCastException exception) { string message = "MembershipAgeValidation use is relegated to CustomerFormViewModel."; Debug.Assert(false, message); Debug.Assert(false, exception.Message); throw new ValidationException(message); } catch (Exception exception) { Debug.Assert(false, "MembershipAgeValidation unknown exception."); Debug.Assert(false, exception.Message); throw; } }
public void RequiredAndUnacceptableAge() { int membershipType = MembershipType.Monthly; DateTime customerBirthDate = new DateTime(2018, 8, 28); MembershipAgeRequirements requirements = new MembershipAgeRequirements(); BusinessRulesResult result = requirements.IsCustomerAgeAcceptable(membershipType, customerBirthDate); Assert.IsTrue(result.IsErrored); membershipType = MembershipType.Quarterly; result = requirements.IsCustomerAgeAcceptable(membershipType, customerBirthDate); Assert.IsTrue(result.IsErrored); membershipType = MembershipType.Yearly; result = requirements.IsCustomerAgeAcceptable(membershipType, customerBirthDate); Assert.IsTrue(result.IsErrored); }
public void NotRequired() { int membershipType = MembershipType.PayAsYouGo; MembershipAgeRequirements requirements = new MembershipAgeRequirements(); BusinessRulesResult result = requirements.IsCustomerAgeAcceptable(membershipType, null); Assert.IsFalse(result.IsErrored); DateTime customerBirthDate = new DateTime(2018, 8, 28); result = requirements.IsCustomerAgeAcceptable(membershipType, customerBirthDate); Assert.IsFalse(result.IsErrored); customerBirthDate = new DateTime(1960, 8, 28); result = requirements.IsCustomerAgeAcceptable(membershipType, customerBirthDate); Assert.IsFalse(result.IsErrored); }