How to validate Class properties?

2020-06-23 07:20发布

Background:

I have a CSV file, which I need to ready and validate each element in each row and create a collection of a class, having valid data.

i.e CSV File looks like:

   EmpID,FirstName,LastName,Salary
    1,James,Help,100000
    2,Jane,Scott,1000
    3,Mary,Fraze,10000

Class looks like:

public class Employees
    {
        public int EmpID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Salary { get; set; }
        public string ErrorReason { get; set; }
    }

Here are the validations required for each field:

  • EmpID:

    • Its a mandatory field, hence cannot be null or empty
    • It should be only an integer
    • It should be not more than 2 digits
    • It should be present in database (query that database and check if an employee exits with this empid.
  • FirstName (same validation for LastName):

    • Its a mandatory field, hence cannot be null or empty
    • It should be only alphabets.
    • Not more than 30 characters are allowed
  • Salary:

    • Its a mandatory field, hence cannot be null or empty
    • It should be a decimal.

To achieve this, here is my approach:

  1. Read CSV file row by row
  2. For each element i.e EmpId, FirstName... etc do the required validations by calling individual methods having validation logic. eg: public bool ValidateIsDecimal(string value) { } public bool ValidateIsEmpIdExists(string value) { } etc
  3. If valid, fill corresponding property of "Employees" class.
  4. If NOT VALID, fill "ErrorReason" property, with appropriate reason as to what caused the validation to fail.(eg: Required filed was missing or datatype is not decimal etc)
  5. Add this Class to Employees collection (eg: List)

So, my question is, is this the right approach, or is there any other better/cleaner way of validating class properties.

Thanks

1条回答
家丑人穷心不美
2楼-- · 2020-06-23 07:52

I would consider using the C# DataAnnotations namespace. I use them often for MVC models and they are very helpful.

The reason I think this would be helpful is that you can attempt to create a new Employees object in a try/catch block and catch ValidationExceptions such as:

List<Employees> empList = new List<Employees>();

foreach (var row in csvRows){
    try {
        //Parse the row here and create the object. don't do any validation here
        var employee = CreateEmployeeFromRow(row);  
        empList.Add(employee);
    }
    catch (ValidationException ve){
        //do whatever
    }
}

Your class would look like:

using System.ComponentModel.DataAnnotations;

public class Employees
{
    [Required, RangeAttribute(0, 99)]
    public int EmpID { get; set; }
    [Required, Length(30), RegularExpression("/^[A-Za-z]+$/")]
    public string FirstName { get; set; }
    [Required, Length(30), RegularExpression("/^[A-Za-z]+$/")]
    public string LastName { get; set; }
    [Required]
    public decimal Salary { get; set; }
}

As far as avoiding duplicate employee IDs, I would just check that before you insert into your database. That doesn't really make an Employees object invalid (or the line in the CSV invalid because it is the proper format).

查看更多
登录 后发表回答