Using a domain class as input model is bad?

Asked

Viewed 18 times

0

my first question here. Well my question is more related to code design.

Let’s say you developed a c# Rest API using . net core and you have in your domain a class called Pessoa and you have a controller who receives a request of the type POST to register a person, in which case you would reuse that same class Pessoa to process the data coming in the request or you would create a classe of the kind PessoaInputModel to be responsible for it there. Example:

Controller:

public class PessoaController{

private IMeuRepositorio _meuRepositorio;

PessoController(IMeuRepositorio meuRepositorio){
    _meuRepositorio =  meuRepositorio;
}


[POST]
public async Task<IActionResult> CadastrarNovaPessoa([FromBody] Pessoa novaPessoa){
    if(await _meuRepositorio.CadastrarPessoaAsync(novaPessoa) is var resultado && resultado.EhFalha)
        return badRequest();
    return Created();            
}

}

Dorian class

public class Pessoa{
Pessoa(string nome, string sobrenome, int idade){
    Nome = nome;
    Sobrenome = sobrenome;
    Idade = idade;
}

public String Nome {get; set;}
public String Sobrenome {get; set;}
public int Idade {get; set;}

public String RecuperarNomeCompleto(){
    return $"{Nome} {Sobrenome}";
}

}

Input model class

public class PessoaInputModel {
public String Nome {get; set;}
public String Sobrenome {get; set;}
public int Idade {get; set;}

}

In the place where I work, some domain classes don’t have behaviors, they only have data but they don’t have any method with some intelligence so it kind of gives me a sense of code duplication that wouldn’t make much sense to have.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.