What is the best validation strategy before the data persist? [closed]

3

I'm used to .NET using C # and EntityFramework.

And there in Entity we have dataanotations . When I create the entity in C #, for example of person, just put the datanotaations and the Entity is valid for me:

public partial class Tab_Parente
{
  [Key]
  public int Cpf { get; set; }

  [Display(Name = "Nome da Pessoa")]
  [Required(ErrorMessage = "O Nome Aluno deve ser informado.")]
  publicint Nome{ get; set; }

  [Display(Name = "CPF")]
  [StringLength(15, ErrorMessage = "O CPF deve ter no máximo 15 caracteres.")]
  public string Cpf { get; set; }
}

But I started on a project that requires ADO. Then the question came to me: What is the best way to validate data before persisting with ADO?

Will I have to create a validation method for each field and call all of them before sending a command of update or insert to the bank?

Type to see if the ID already exists with a select . See if the entered CPF has more than 15 characters, etc., all at hand?

-------------------- EDITION --------------------- I found my answer:

Well contrary to what I thought and stated previously DataAnnotations can be used in WindowsForms applications.

Many people do not know but Data Annotations are not exclusive to MVC, nor much less restricted to the Entity Framework. Following a Macoratti tutorial on using: Validator.TryValidateObject (obj, context, resultValidation, true) I was able to construct a getValidationErros class (object obj), which returns an error list for an entity. In my Windows Forms project and using pure ADO.

See how it went:

Entity

public class HoldingError Group: GeneralEntity, IHoldingErnt Group {     [Display (Name="Group Code", Description="Group Code.")]     [Required (ErrorMessage="000001 - Code Group can not be empty in Holding Group.")]     [RegularExpression (@ "^ [0-9] * $", ErrorMessage="000002 - Only numbers (from 0 to 9) are allowed for Group Code in Holding Group.")]     public string CodeGroup {get; set; }

[Display(Name = "Nome Fantasia", Description = "Nome completo.")]
[Required(ErrorMessage = "O nome fantasia é obrigatório.")]
public string NomeFantasia { get; set; }

public IHoldingGrupoStatusRegistroEnt HoldingGrupoStatusRegistroEnt { get; set; }

public DateTime? DataCadastro { get; set; }

} Class Validation

public static class Validation {     public static IEnumerable getValidationErros (object obj)     {         var resultValidation = new List ();         var context = new ValidationContext (obj, null, null);         Validator.TryValidateObject (obj, context, resultValidation, true);         return resultValidacao;     } } Method in Entity Context (where are the Update, insert, etc methods)

public override bool ValidateRegistration ()     {         DataData = false;

    if (DadosRecebidos)
    {
        var erros = Validacao.getValidationErros(ObjetoEnt);
        foreach (var error in erros)
        {
            MessageBox.Show((error.ErrorMessage));
        }
    }
    //return DadosValidados = (erros.ToString() = 0) ;
    return DadosValidados = true;
}

In addition, I'm still not sure how, but I've already been told that Fluent Validation can also be used outside the MVC world.

    
asked by anonymous 23.10.2017 / 20:08

1 answer

6

Basically this is it. Of course it does not necessarily have to be a method for each validation, it can be a method that does all. This is a question of project organization. Each project may require a different solution.

Or you can create a EF-like framework to automate this and have similar behavior where you can just annotate and use it. The annotations even exist, but you have nothing to do the necessary actions when you find these notes. Certainly there are some ready ones who can already do such a task.

    
23.10.2017 / 20:19