Pedrof, though lavishly discounting his choice of architecture (Entity Framework with Repository Pattern/DAL ), I will try to help you with the validation.
First, make your entity, in case Player inherits from IValidatableObject, then implement your validation rule in the Validate method().
public class Jogador : IValidatableObject
{
public string Nome { get; set; }
public int Altura { get; set; }
public DateTime? DataNascimento { get; set; }
public string Nacionalidade { get; set; }
public int NumeroCamisola { get; set; }
public int Peso { get; set; }
public string Posicao { get; set; }
public bool Ativo { get; set; }
public IEnumerable<ValidationResult>(ValidationContext validationContext)
{
if (this.Nome == null)
yield return new ValidationResult("Propriedade Nome é obrigatoria", new [] { "Nome" });
if (this.Altura == 0)
yield return new ValidationResult("Propriedade Altura é obrigatoria", new [] { "Altura" });
if (this.DataNascimento == null)
yield return new ValidationResult("Propriedade DataNascimento é obrigatoria", new [] { "DataNascimento" });
if (this.Nacionalidade == null)
yield return new ValidationResult("Propriedade Nacionalidade é obrigatoria", new [] { "Nacionalidade" });
if (this.NumeroCamisola == 0)
yield return new ValidationResult("Propriedade NumeroCamisola é obrigatoria", new [] { "NumeroCamisola" });
if (this.Peso == 0)
yield return new ValidationResult("Propriedade Peso é obrigatoria", new [] { "Peso" });
if (this.Posicao == null)
yield return new ValidationResult("Propriedade Posicao é obrigatoria", new [] { "Posicao" });
if (this.Ativo == false)
yield return new ValidationResult("Propriedade Ativo é obrigatoria", new [] { "Ativo" });
}
}
In the example above, as we are only checking if the field has been filled in, the ideal would be to use Dataannotations, so the implementation below would also be valid:
public class Jogador : IValidatableObject
{
[Required(ErrorMessage = "Propriedade Nome é obrigatoria")]
public string Nome { get; set; }
[Required(ErrorMessage = "Propriedade Altura é obrigatoria")]
[Range(100, 250, ErrorMessage = "Altura deve ser maior que 1.00 e menor que 2.50")]
public int Altura { get; set; }
[Required(ErrorMessage = "Propriedade DataNascimento é obrigatoria")]
public DateTime? DataNascimento { get; set; }
/* Outras Validacoes */
public IEnumerable<ValidationResult>(ValidationContext validationContext)
{
var results = new List<ValidationResult>();
Validator.TryValidateProperty(this.Nome,
new ValidationContext(this, null, null) { MemberName = "Nome" },
results);
Validator.TryValidateProperty(this.Altura,
new ValidationContext(this, null, null) { MemberName = "Altura" },
results);
Validator.TryValidateProperty(this.DataNascimento,
new ValidationContext(this, null, null) { MemberName = "DataNascimento" },
results);
/* Demais Validações */
if (this.DataNascimento.HasValue && (this.DataNascimento.Value.Year < 1900 || this.DataNascimento.Value.Date > DateTime.Today))
results.Add(new ValidationResult("Propriedade DataNascimento é invalida", new [] { "DataNascimento" }));
/* Demais Validações */
return results;
}
}
Validation is performed automatically when calling Savechanges();
//Input.: DataNascimento = 01/01/1800 (Data Invalida)
public static void CriaRegisto(JogadorDTO jogadorDTO)
{
var tabelaJogador = Mapper<Jogador>(jogadorDTO);
using (var baseDados = new bd_AvesEntities())
{
baseDados.tbJogadores.Add(tabelaJogador);
baseDados.SaveChanges(); //Validation Exception
}
}
If you prefer, you can also call the method .GetValidationErrors()
//Input.: DataNascimento = 01/01/1800 (Data Invalida)
public static IEnumerable<ValidationResult> CriaRegisto(JogadorDTO jogadorDTO)
{
var tabelaJogador = Mapper<Jogador>(jogadorDTO);
using (var baseDados = new bd_AvesEntities())
{
baseDados.tbJogadores.Add(tabelaJogador);
var erros = baseDados.GetValidationErrors();
if (!erros.Any())
{
baseDados.SaveChanges();
}
return erros;
}
}
In your presentation layer, if using ASP.NET MVC, how are you using a DTO as Model, to JogadorController will not validate automatically, so you will have to add the ValidationResult to his ModelState.
To display errors on the page, simply make use of HttpHelper.ValidationMessageFor, for example:
<div>
@Html.LabelFor(model => model.Nome)
</div>
<div>
@Html.EditorFor(model => model.Nome)
@Html.ValidationMessageFor(model => model.Nome)
</div>
If you are using other technology in your presentation layer, you will need to decide how best to display the IEnumerable<ValidationResult> for the User.
I believe that the best way to validate the screen fields would be with Jquery [https://jquery.com/]
– hard123