0

I am starting a new Web API project and trying to still grasp the concepts of DTO / View Model.I know for a fact that your DTOs should only hold data and any required business rules should be performed on the DTO before finally it gets to the controller to be converted (mapped) to an appropriate view model.

However, in my case the PutUser action expects an entire "UpdateUserViewModel" in the form of Json from the client :

public HttpResponseMessage PutUser(UpdateUserViewModel user)
{          
  var userDTO = UserManager.Update(user); // Passing the viewmodel as it is to the business manager
  // Perform DTO to view model mapping here and return response.
  return Request.CreateResponse(HttpStatusCode.OK,UpdateUserViewModel);
}

In my business layer I will now map this view model user to the userDTO and perform any business logic and return a userDTO object to the Action which will then be mapped to the view model and returned as the response , is this the right approach or should my manager only expect a DTO object, basically where should the mapping of ViewModel -> DTO happen -> ViewModel ?

If this is the right approach, what is the best way to map ViewModel entities to DTO and the reverse without using auto mapper?

Murtaza Mandvi
  • 10,708
  • 23
  • 74
  • 109
  • 1
    Data transfer objects do not contain any logic. So by definition, if validation or business rule application is to occur, it must occur somewhere else, not in the DTO. In your particular case, the DTO seems redundant. Are you using the returned DTO object anywhere? Did you leave that code out? – Robert Harvey Oct 26 '14 at 23:41
  • In addition, I don't know what you mean by the "best" way. What do you mean by "best?" The fastest performing? The fewest number of lines of code? The approach that best meets your software requirements? – Robert Harvey Oct 26 '14 at 23:43
  • The DTO object is only used inside UserManager.Update(user) where it will interact with the database in this example, however I have other actions where the DTO has to go through some business rules inside the maanger before getting updated to the DB. – Murtaza Mandvi Oct 26 '14 at 23:44
  • Seems like as good an approach as any. – Robert Harvey Oct 26 '14 at 23:45
  • Since I do not want to use the automapper I just wanted to know what is the "correct" way of mapping otherwise, wrong choice of word at my end :) – Murtaza Mandvi Oct 26 '14 at 23:46
  • Since you are OK with this approach, my only question is, since the life of a ViewModel object should be only inside the controller, in my case it seems it goes beyond the controller into my business manager, where it does get converted to DTO, is that OK as well? – Murtaza Mandvi Oct 26 '14 at 23:48
  • May I ask why are you getting a ViewModel from the client? as I see it, it is a DTO as well - transferring data from the client. unless I'm missing something here. – Ron.B.I Oct 26 '14 at 23:50
  • @Ron.B.I Indeed, the only reason my DTO is separate from my view model is since I have some extra fields, related to business rules that get populated once I get the viewmodel and depending on the user that field's value varies, however that field is never visible to the end user. – Murtaza Mandvi Oct 26 '14 at 23:52
  • Which approach best meets your software's requirements? If they all are adequate, which approach is simplest? – Robert Harvey Oct 27 '14 at 00:03

0 Answers0