Hi I'm new in C# and I have created a WPF form that takes in a csv and save it in the db. Now I was wondering if there are a Validation Library
that I can use to make those field checking easier? Like I would like to implement a field required or something and it will throw an error if the field is not required. How can I achieve this? I have read some code that has something like this
[Required] // Not sure if this is the correct format
public string Name()
{
get;set;
}
So basically I was thinking of a middleware type of validation where I just place the [Required]
attribute in the model class and it will do the validation for me. Is there such a thing?
Update: What I mean by validating is that when after I select a csv file, it will read through per row/column now each column will be validated like ex. For column[0]
(assuming this is field for Name) then when this field is read and assign to a variable it should be validated before proceeding in assigning to a variable. Not sure if this is possible more it's something like this
string[] content = row.Split('|');
Customer customer = new Customer(content);
Lis<KeyValuePair<string, string>> rowList = Converter.ToKeyValuePair(customer);
And here's the code for the Customer
class
class Converter
{
public static List<KeyValuePair<string,string>> ToKeyValuePair(Customer customer)
{
List<KeyValuePair<string, string>> rowList = new List<keyValuePari<string, string>>();
rowList.Add(new KeyValuePair<string, string>('name', customer.Name));
}
return rowList;
}
// Class Customer
class Customer
{
private string _name;
public Customer(string[] content)
{
this.Name = content[0];
}
[Required] // Maybe some auto validation here where when the property is accessed it should be validated immediately
public string Name
{
get { return _name; }
set {
// I'm thinking of some validation here?
_name = value;
}
}
}
As you can see in my example I wanted that whenever the property like this.Name = content[0]
is accessed it automatically validate if the content has value or not