16
I have an ASP NET MVC 4 project with the following projects:
- Domain
- Repository
- Contracts (interfaces)
- DTO’s
- And the web project
The web project "sees" only the repository project and it is responsible for executing the business rules. This project always returns DTO’s to the web layer and in the web layer (controllers) I turn the DTO into a viewmodel and return the viewmodel to the view.
I’m using DTO’s because in most queries I don’t need all the data from the entities, so I don’t need to expose my entire entity to the view (and I also think it’s not a good practice).
The problem I am seeing is that it is redundant to return a DTO to my web layer and in the web layer turn the DTO into viewmodel. Below is an example:
Suppose the repository has a method that returns user data (login and email) by id:
Return DTO of the method:
public class UsuarioDto{
public string Login {get; set;}
public string Email {get set;}
}
Method in the repository that returns the user to the web layer
public class UsuarioRepositorio : IUsuarioRepository
{
public UsuarioDto GetUsuario(int usuarioId){
using(var context = new dbContext()) //instancia do contexto do entity
{
return context.Usuario.Select(x => new UsuarioDto{
Login = x.Login,
Email = x.Email
}
).FirstOrDefault(x.id == usuarioId);
}
}
}
At the point below I think I’m being redundant when turning DTO into viewmodel
Viewmodel representing the user (inside the Models folder)
public class UsuarioViewModel{
public string Login {get; set;}
public string Email {get set;}
}
Controller
public class HomeController : Controller
{
public ActionResult User(int usuarioId)
{
UsuarioRepositorio usuarioRepositorio = new UsuarioRepositorio();
var usuario = usuarioRepositorio.GetUsuario(usuarioId)
.Select(x => new UsuarioViewModel{
Login = x.Login,
Email = x.Email
}
);
return View(usuario)
}
}
Is there a way for me to optimize this transformation from DTO to Viewmodel? The impression I have is that I am duplicating code, because as far as I know, the DTO serves to traffic data between layers and the viewmodel is used to expose the data to the view.
Stop using DTO for some cases? If I don’t use DTO what could I use? Return my model to the view? In this case I think there should be a standard pq if sometimes use DTO and sometimes not the project can get confused. Another point. My user table has 15 fields. If I want to recover only 2 I use DTO. I see no reason to return all entities.
– Tiago Crizanto
@Thiagocrizanto Stop using DTO for some cases? Yes. If I don’t use DTO what could I use? Your repository as exemplified. Return my model to the view? Yes. Using the pattern presented you will have the redundancy to do from/to. Then it is up to you to check what is best: to have a pattern and to be doing/to or using DTO only when really necessary. By filtering (p/ not returning all entities) in the repository methods you can filter the fields/entities you want, you don’t need DTO just for that. It may get confused if you don’t understand how to really use DTO.
– Renan