I'm working on a prototype of an ASP.NET MVC application, where I want to leave my lean controller (with the least amount of code possible) for that, not doing business logic on it, but yes in the business layer.
I have the business layer, where I have my class Usuario
, and in this layer I have interfaces that should be implemented by other layers. I'll be using dependency injection.
My question is how to notify my controller of what worked or not, to notify or redirect the user, for example, when my controller calls Usuario.Cadastrar()
, there may be an error filling the data, the password may not meet the security requirements, the email may already be registered or there may be an error in sending the confirmation email.
What should the Register method return to the Controller ? An enumerator like ( ErroEnviarEmail
, SenhaFraca
, EmailCadastrado
), a Exception
for each possible path? A string etc.
public class Usuario
{
public Usuario()
{
}
public Usuario(IUsuarioRepositorio repositorio, ISeguranca seguranca, IEnviaEmail email)
{
this.repositorio = repositorio;
this.seguranca = seguranca;
this.email = email;
}
private IUsuarioRepositorio repositorio;
private ISeguranca seguranca;
private IEnviaEmail email;
public int Codigo { get; set; }
public string Email { get; set; }
public string Senha { get; set; }
public bool Cadastrar()
{
if (this.ValidarPreenchimento())
{
if (this.seguranca.ValidarSegurancaSenha(this.Senha))
{
if (!this.repositorio.EmailCadastrado(this.Email))
{
this.Senha = seguranca.Criptografar(this.Senha);
this.repositorio.Inserir(this);
this.email.EnviarEmailCadastro(this);
return true;
}
}
}
return false;
}
private bool ValidarPreenchimento()
{
if (string.IsNullOrEmpty(this.Email))
return false;
if (string.IsNullOrEmpty(this.Senha))
return false;
return true;
}
}