What is the difference between a view model and a data transfer object?

I base this question on Fowler PoEAA. Given your familiarity with this text, aren't ViewModels used in ASP.NET MVC the same as DTO? Why or why not? Thank.

+38
design-patterns asp.net-mvc software-design poeaa
Sep 16 '09 at 7:13
source share
4 answers

They perform a similar task (encapsulating data for a different application level), but they do it in different ways and for different reasons.

  • The purpose of the DTO is to reduce the number of calls between application layers, especially when these calls are expensive (for example, distributed systems). DTOs are almost always trivially serializable and almost never contain any behavior.

    For example, you are developing an e-commerce site. CreateCustomer and AddCustomerAddress are separate operations at the database level, but you may want to combine your data in NewCustomerWithAddressDto for performance reasons, so that your client only needs to make one round-trip to the server, t need to take care that the server can do a bunch different things with a data packet.

  • The term "ViewModel" means slightly different things in different flavors of MV *, but its purpose is mainly to separate the problems. Your model is often optimized for a purpose other than presentation, and ViewModel's responsibility is to separate your view from the implementation details of the model. In addition, most MV * templates advise you to make your views “as deep as possible,” and therefore the ViewModel sometimes takes responsibility for the presentation logic.

    For example, in the same e-commerce application, your CustomerModel is the wrong presentation form in the New Customer view. Firstly, your view has two form fields so that your user can enter and confirm their password, and your CustomerModel does not contain a password field at all! Your NewCustomerViewModel will contain these fields and may, depending on your taste MV *, be responsible for any presentation logic (for example, show / hide parts of the view) and basic verification (for example, make sure that both password fields match).

+68
Sep 16 '09 at 7:28
source share

The goal is different:

  • DTO are used for data transfer.
  • ViewModels are used to display data to the end user.

Thus, usually ViewModels contain presentation data, because in many cases this is similar to what is in the DTO, but with some differences. Think about the presentation of transfers, localization, currency, date formats, .... This is due to the fact that your presentation should not have any logic.

+9
Sep 16 '09 at 7:18
source share

DTOs in MVVM and MVP are usually very dumb objects and are basically just a collection of properties and getters. ViewModels, on the other hand, may have some kind of behavior.

The practical positive side effect of using DTO is to simplify serialization. If you have a rather complex object, say, C #, you will often be forced to selectively disable everything that you do not need to serialize. It can get pretty ugly, and DTOs make this process easier.

+9
Sep 16 '09 at 7:52
source share

The view model and the data transfer object have similarities and differences.

Similar: Transferring data to a record (an instance of an object, possibly serialized) to a receiver, be it a view or a service

Difference: The view model is intended to be sent to the view where it will be displayed, with formatting. The view model also sends data back to the controller. DTO is not usually intended for presentation. It is designed to send raw data.

+1
Jan 24 '13 at 19:33
source share



All Articles