I question where is easily the most convenient spot to validate a house length on the persistent object.

Let us say, there's a table known as Country during my Db with CountryCode nvarvhar(3).

And I've got a planned object Country wiht property CountryCode which may be saved into Db.

Where must i see if the nation code set by user doesn't exceed 3 figures:

  • Within the setter of property CountryCode
  • OR during the time of saving into Db

Are you able to please advice?

Thanks

Based on code secure recomendations, all inspections ought to be carried out as soon as possible. Furthermore, it's suggested that every party works the inspections alone without depending on more events verifications.

I have found that it's simplest to permit qualities to become set to the value (from the correct data type anyway) after which validate it before saving it.

I love to make use of the validation characteristics included in .Internet. This prevents the logic connected using the property. There's a StringLengthAttribute class which should have the desired effect for what you're requesting.

The characteristics have been in the machine.ComponentModel.DataAnnotations namespace (you will need to reference the set up of the identical title).

MVC and EntityFramework possess a built-in method to validate the information. If you want to carry out the logic yourself, here's some code that can help you receive began...

var ctx = new ValidationContext(obj, null, null);
Validator.ValidateObject(obj, ctx);