vendredi 29 janvier 2021

What is the difference between using MaxLengthAttribute and a validation in the setter of the property? [closed]

Which is the difference between using an attribute like

[MaxLength(15)]
public string name{get;set;}

to validate this property and putting a validation code in the setter of the property.

public string name{get;
  set{
    if(value.Length > 15){throw new error... } };
}

Why we should use reflection and attributes when we can validate the property in the setter and its easier than using reflection and an attribute, on run time we instantiate the class, we wait for the value input from the user and if the value will not be valid will not be stored.

So why we should use an attribute in this case and no validation in the setter which is easier?





Aucun commentaire:

Enregistrer un commentaire