Why might we want to validate our data? Well, for starters, our app may need to identify some fields to the user as required. We might also want to inform the user that they entered an invalid number for the original gravity of a recipe, or that they entered text and not a number at all. The feedback to the user not only protects our app from capturing invalid or malicious data, it also provides a better user experience by notifying the user an error has occured before an attempt is made to persist the data.
Our data validation will be accomplished by leveraging the data annotation attributes.
The data annotations, defined in System.ComponentModel.DataAnnotations
, have already been leveraged to some extent in our app on the view models to provide display and formatting hints to the view renderer. You can see this in the following piece of code:
[Display(Name = "Original Gravity")] [DisplayFormat(DataFormatString = "{0:0.00##}")] public float OriginalGravity...