I am creating a solution where I would like to keep all business rules within my models including the error response. The problem is that I would also like to be able to capture multiple errors at once like this:
public class Usuario
{
private int idade;
public int Idade
{
get => idade;
set
{
if (value < 18)
throw new ArgumentOutOfRangeException("O usuário precisa ter mais que 18 anos.");
idade = value;
}
}
private string senha;
public string Senha
{
get => senha;
set
{
if (string.IsNullOrWhiteSpace(value) || value.Length < 6 || value.Length > 50)
throw new ArgumentException("A senha deve ter entre 6 e 50 caracteres.");
senha = value;
}
}
}
I need to make a new instance of this user and feed this information, so that I can return all errors regarding the implementation of the template so that they can be used to inform the user of the model ( which can be an API, a view , or any other), like this:
public class PropertySetError
{
public string Name { get; set; }
public ICollection<string> Errors { get; set; }
}
public class Response<T>
{
public bool Status { get; set; }
public ICollection<PropertySetError> InvalidProperties { get; set; }
}
public class UsuarioController : ControllerBase
{
[HttpPost("/usuario")]
public async Task<ActionResult<Response<Usuario>>> postUsuario([FromBody] JObject obj)
{
Usuario usuario = new Usuario();
object temp = null;
Response<Usuario> response = new Response<Usuario>();
try
{
foreach(var prop in typeof(Usuario).GetProperties())
{
try
{
obj.TryGetValue(prop.Name, out temp);
prop.SetValue(usuario, Convert.ChangeType(temp, prop.GetType()));
}
catch (Exception)
{
throw;
}
}
response.Status = true;
return response;
}
catch (Exception ex)
{
response.Status = false;
// ... pega todos os possíveis erros aqui e passa para a resposta
return response;
}
}
}