1
I’m creating a relatively simple class, but I’ve included some validations in its properties.
public class Sala : EntidadeBase
{
#region Enums
public enum StatusSala
{
Disponivel,
Reservada,
Inativa,
Bloqueada
};
#endregion
#region Propriedades
public string Nome { get; private set; }
public int Capacidade { get; private set; }
public StatusSala Status { get; private set; }
#endregion
#region Contrutores
public Sala(string nome, int capacidade, StatusSala status) : base()
{
SetNome(nome);
SetCapacidade(capacidade);
SetStatus(status);
}
#endregion
#region Métodos
#region SettersPrivados
private void SetNome(string nome)
{
ValidacaoDominio.Validar()
.Quando(string.IsNullOrEmpty(nome), "O nome não pode ser vazio")
.Quando(!string.IsNullOrEmpty(nome) && nome.Length < 3, "o nome deve ter pelo menos 3 caracteres")
.DispararValidacao();
Nome = nome;
}
private void SetCapacidade(int capacidade)
{
ValidacaoDominio.Validar()
.Quando(capacidade < 1, "Capacidade da sala Inválida")
.DispararValidacao();
Capacidade = capacidade;
}
private void SetStatus(StatusSala status)
{
ValidacaoDominio.Validar()
.Quando(!Enum.TryParse(typeof(StatusSala), status.ToString(), out var retorno), "Status Inválido")
.DispararValidacao();
Status = status;
}
#endregion
#region Publicos
public void AlterarNome(string nomeAlterado)
{
SetNome(nomeAlterado);
}
public void AlterarCapacidade(int capacidadeAlterada)
{
SetCapacidade(capacidadeAlterada);
}
public void AlterarStatus(StatusSala statusAlterado)
{
SetStatus(statusAlterado);
}
#endregion
#endregion
As a matter of organisation, I would not like to leave a string fixed in the validation messages, as some can be repeated (example: Invalid name. This can be referenced to any entity that owns the Name property) and the possibility to escape from the pattern is large.
As far as I know, I can do that Resources or create a helper class to concentrate these messages.
Initially, I concentrated the messages in static classes, separating them by categories (from the most general to the most specific), but nothing prevents to do otherwise.
Class for "general messages":
public class GeralMsgs
{
public const string NomeInvalido = "O Nome é invalido";
public const string NomeMenorTresCaracteres = "O nome deve ter pelo menos três caracteres";
}
Class for class specific messages Sala
:
public class SalaMsgs
{
public const string CapacidadeInvalida = "A capacidade da sala é inválida";
public const string StatusInvalido = "O status da sala é inválido ";
}
In this way, the validations would reference the constants
ValidacaoDominio.Validar()
.Quando(string.IsNullOrEmpty(nome), GeralMsgs.NomeInvalido)
.Quando(!string.IsNullOrEmpty(nome) && nome.Length < 3, GeralMsgs.NomeMenorTresCaracteres)
.DispararValidacao();
...
ValidacaoDominio.Validar()
.Quando(capacidade < 1, SalaMsgs.CapacidadeInvalida)
.DispararValidacao();
...
ValidacaoDominio.Validar()
.Quando(!Enum.TryParse(typeof(StatusSala), status.ToString(), out var retorno), SalaMsgs.StatusInvalido)
.DispararValidacao();
From the health point of view of the application, thinking about scalability and good practices, what is the right place to concentrate these messages? I leave it that way or I play it all for a file Resource of the project or whatever?
My goal is always to try to do the best possible way, but without creating a laser gun to kill an ant, but also do not want in the future to look at the 147 that the Maniero always put and realize that my application or mine mindset led me to the "Important is to work".
"147 that Maniero always put" what is 147?
– CypherPotato
https://pt.wikipedia.org/wiki/Fiat_147
– Eduardo Oliveira
kkk o fiat 147 which represents "working is different from being right"->https://answall.com/a/42262626/69359
– Rovann Linhalis
I always used from the beginning this bookstore and never again have I seen anything with as many lines of code as it shows :) it’s simple, each class gets its validator, helps maintenance in the future...
– balexandre
@balexandre, I traded the current validation for this approach, but using Notify Me, which is a Flunt-based package, from Andre Baltieri, but I still have the same problem that is looking for where I should include the validation messages.
– Eduardo Oliveira