I'm building an application following the DDD standards. I would like to know how to correctly use the default Data Transfer Object - DTO.
With the little I read, I came to the conclusion: I'm using ViewModel (a representation of the entity to be used in the View), so I need to convert that ViewModel to DTO to cast to the other layers. Coming to the domain, I need to convert back from DTO to entity and send the entity to the repository (persistence). The reverse path is also required in the query case.
The question is: Can I have a constructor in the ViewModel that receives a DTO and creates a ViewModel from it? And in DTO a constructor that creates a DTO from a ViewModel? The same for the entity.
For example:
public Class UsuarioViewModel{
public string Nome {get; set;}
public string EMail {get; set;}
public string Senha {get; set;}
public UsuarioViewModel(){
}
public UsuarioViewModel(UsuarioDTO _usuariodto){
Nome = _usuariodto.Nome,
EMail = _usuariodto.EMail,
Senha = _usuariodto.Senha
}
}
public Clas UsuarioDTO{
public string Nome {get; set;}
public string EMail {get; set;}
public string Senha {get; set;}
public UsuarioDTO(){
}
public UsuarioDTO(UsuarioViewModel _usuariovm){
Nome = _usuariovm.Nome,
EMail = _usuariovm.EMail,
Senha = _usuariovm.Senha
}
public UsuarioDTO(Usuario _usuario){
Nome = _usuario.Nome,
EMail = _usuario.EMail,
Senha = _usuario.Senha
}
}
public Clas Usuario{
public string Nome {get; set;}
public string EMail {get; set;}
public string Senha {get; set;}
public Usuario(){
}
public UsuarioDTO(UsuarioDTO _usuarioDTO){
Nome = _usuarioDTO.Nome,
EMail = _usuarioDTO.EMail,
Senha = _usuarioDTO.Senha
}
}
// Controller
_serviceApp.Add(new UsuarioDTO(usuarioVM));
//Service
_service.Add(new Usuario(usuarioDTO));
//Repositorio
_repositorio.add(Usuario usuario);
Can I work this way?