I have an application which runs as a windows service and reads a text data file, then maps the file contents to a database. The app uses a batch commit and periodically the data is larger than the database fields.
What is the best way to validate that the data fields are not too big before trying to save them to a database?
If you are willing to use System.ComponentModel.DataAnnotations you can do something like this.
The Validator.ValidateObject will throw an exception in this case because the Name field is too big which is enforced by the
StringLengthattribute.You can also use the Validator.TryValidateObject which will return you a list of errors instead of throwing an exception.
The validation framework is quite powerful. You can use regular expression validation for strings. Range validation for number fields and even custom validation.