static void AddCustomerWithFluentValidation(AddCustomerDto dto) { var validator = new AddCustomerDtoValidator(); var validRes = validator.Validate(dto); if (!validRes.IsValid) { //first error message Console.WriteLine(validRes.Errors.FirstOrDefault()); ////all error messages //Console.WriteLine(validRes.ToString(",")); //Console.WriteLine(string.Join(",", validRes.Errors.Select(x => x.ErrorMessage))); //Console.WriteLine(string.Join(",", validRes.Errors)); } //save code ... }
static void Main(string[] args) { var addCusDto = new AddCustomerDto(); AddCustomer(addCusDto); AddCustomerWithFluentValidation(addCusDto); var addCusDto2 = new AddCustomerDto2 { Address = new Address(), Hobbies = new List <string> { "", "coding" } }; ListAndIncludeValidation(addCusDto2); Console.ReadKey(); }
static void AddCustomer(AddCustomerDto dto) { if (string.IsNullOrWhiteSpace(dto.Name)) { Console.WriteLine($"name cannot be null"); return; } if (string.IsNullOrWhiteSpace(dto.Phone)) { Console.WriteLine($"phone cannot be null"); return; } if (dto.Other <= 0) { Console.WriteLine($"other must great than 0"); return; } //save code ... }