/// <summary> /// Validates the given Book object. /// </summary> /// <param name="book">The book.</param> /// <returns>True or False.</returns> public bool IsValidBook(Book book) { bool isValid = true; ValidationContext context = new ValidationContext(book); IList <ValidationResult> validationResults = new List <ValidationResult>(); isValid = Validator.TryValidateObject(book, context, validationResults, true); if (validationResults.Count != 0) { foreach (ValidationResult result in validationResults) { ErrorsHandler.Add(result.ErrorMessage); } isValid = false; } if (!isValid) { return(false); } if (book.Domains.Count > Threshold.MaxDomains) { ErrorsHandler.Add(ValidationErrors.TooManyDomains); isValid = false; } if (!isValid) { return(false); } if (AreRelatedDomains(book.Domains)) { ErrorsHandler.Add(ValidationErrors.DomainAreRelated); isValid = false; } return(isValid); }
/// <summary> /// Validates the given Loan object. /// </summary> /// <param name="loan">The loan.</param> /// <returns>True or False.</returns> public bool IsValidLoan(Loan loan) { bool isValid = true; ValidationContext context = new ValidationContext(loan); IList <ValidationResult> validationResults = new List <ValidationResult>(); Validator.TryValidateObject(loan, context, validationResults, true); if (validationResults.Count != 0) { foreach (ValidationResult result in validationResults) { ErrorsHandler.Add(result.ErrorMessage); } isValid = false; } ////if (!isValid) ////{ //// return false; ////} ////if(loan.BorrowedEditions.Count > Threshold.MaxBooks) ////{ //// ErrorsHandler.Add(ValidationErrors.TooManyBooks); //// isValid = false; ////} else ////{ //// if(loan.BorrowedEditions.Count >= 3) //// { //// BookService service = new BookService(); //// ContainsMoreThatTwoDomains(loan.BorrowedEditions); //// } ////} foreach (Edition edition in loan.BorrowedEditions) { if (edition.NoForLoan == 0) { ErrorsHandler.Add(ValidationErrors.NoBooksForLoan); isValid = false; break; } else { if (edition.NoForLoan < edition.NoTotal * 0.1) { ErrorsHandler.Add(ValidationErrors.TooFewBooks); isValid = false; break; } } } ////if (!isValid) ////{ //// return false; ////} return(isValid); }