DTO = ViewModel?
Asked Answered
D

9

133

I'm using NHibernate to persist my domain objects. To keep things simple I'm using an ASP.NET MVC project as both my presentation layer, and my service layer.

I want to return my domain objects in XML from my controller classes. After reading some posts here on Stack Overflow I gather DTOs are the way to go. However, I've also come across posts talking about the ViewModel.

My question: Are Data Transfer Objects and ViewModels the same thing? Or is a ViewModel a kind of sub pattern of a DTO?

Dudeen answered 30/12, 2009 at 19:42 Comment(1)
I think it is relevant to mention that ViewModels in ASP.NET MVC are not 100% equivalent to ViewModels in WPF (MVVM), as most answers mention MVVM and you are working with ASP.NET MVC.Anatolio
S
136

The canonical definition of a DTO is the data shape of an object without any behavior.

ViewModels are the model of the view. ViewModels typically are full or partial data from one or more objects (or DTOs) plus any additional members specific to the view's behavior (methods that can be executed by the view, properties to indicate how toggle view elements etc...). You can look at the viewmodel as all the data for a view plus behaviors. ViewModels may or may not map one to one to business objects or DTOs.

By the way, NHibernate projections come in handy if a certain viewmodel needs a subset of the data from a persisted object.

Selfinduced answered 30/12, 2009 at 19:58 Comment(2)
Can you explain this: "DTO is the data shape of an object without any behavior"?Marlo
Meaning... the DTO class usually only contains properties and does not contain any methods with business logic etc...Selfinduced
W
81

ViewModel in ASP.NET MVC practice is the same as the DTO, however ViewModel in MVVM pattern is different from DTO because ViewModel in MVVM has behaviors but DTO does not have.

Wingback answered 25/7, 2011 at 1:21 Comment(3)
Why should the ViewModel in asp.net mvc be the same as an DTO? That makes no sense. A ViewModel can have a behavior a DTO not. This does not depend on mvc.Gefen
+1 for differentiating between ASP.NET MVC ViewModel and MVVM ViewModel.Pharyngology
@Elisa - The answer to your rather old question is that in ASP.NET MVC, the view invokes Actions on the controller (not a ViewModel) in order to change the Model and View in a stateless manner. Because of this, a DTO shaped to a view is essentially the same as the ViewModel. However, in larger systems with another serialization boundary, a DTO may be beneficial if separate from a ViewModel specifically shaped for the View.Conditioner
A
28

DTO != ViewModel

In the MVVM pattern the ViewModel is used to isolate the Model from the View. To represent the Model you could use simple DTO classes, which again is mapped to a database through e.g. NHibernate. But I've never seen a ViewModel class which is modelled as a DTO.. ViewModel classes mostly have behavior, which DTOs don't have.

Alibi answered 30/12, 2009 at 19:59 Comment(1)
so DTO's can just be structs (or is a class that should mimic the capabilities of a struct)?Halfbreed
D
24

DTO - Data Transfer Objects are exactly as it says, containers for transferring data. They have no behaviour but merely a bunch of setters and getters. Some people make them immutable and just create new ones when needed rather than updating existing ones. They should be serializable to allow transfer across the wire.

Generally DTOs are used to ship data from one layer to another layer across process boundries as calls to a remote service can be expensive so all the required data is pushed into a DTO and transferred to the client in one chunk (coarse grained).

However, some people use the notion of screen bound DTOs (nothing to do with crossing process boundries). Again these are populated with the required data (generally the data required for a particular screen and could be an aggregation of data from various sources) and sent to the client.

http://blog.jpboodhoo.com/CommentView,guid,21fe23e7-e42c-48d8-8871-86e65bcc9a50.aspx

In simple cases as has already been stated this DTO can be used for binding to the view but in more complex cases it would require the creation of a ViewModel and unloading of data from DTO to ViewModel which is obviously more work (when applying MVVM pattern).

So again as already stated DTO!=ViewModel

and

DTO and ViewModel have different purposes in life

Dismantle answered 31/12, 2009 at 13:8 Comment(0)
G
18

For some simple views I'll use my DTO as my models, but as Views become more complex I'll create ViewModels.

For me it is a balance between quickness (using DTO, since I already have 'em) and flexibility (creating ViewModels means more separation of concerns).

Gree answered 30/12, 2009 at 19:46 Comment(1)
Nice pragmatic answer.Ford
C
17

First, the major difference is that ViewModel can have behaviour or methods that DTO Must Not !!!

Second, Using DTO as a ViewModel in ASP.NET MVC make your application tightly coupled to DTO and that's exactly the opposite purpose of using DTO. If you do so, what's the difference using your domain Model or DTO, more complexity to get an anti-pattern ?

Also ViewModel in ASP.NET can use DataAnnotations for validation.

The same DTO can have different ViewModels Mapping, and One ViewModel can be composed from differents DTO (always with object mapping not composition) . because i think it is even worse if you have a ViewModel that contains a DTO, we will have the same problem.

From your presentation layer, think about DTO as a contract, you will receive an object that you have to consider as stranger to your application and don't have any control on it (even if you have ex the service, the dto and presentation layers are yours).

Finally if you do this clean separation, developpers can work together with ease. The person who design ViewModels, Views and Controllers don't have to worry about the service layer or the DTO implementation because he will make the mapping when the others developpers finish their implementation... He can even use Mocking tool or manual mocking to fill the presentation layer with data for test.

Corotto answered 27/12, 2012 at 18:9 Comment(2)
I just installed VS 2012 and looked at there MVC 4 Single Page Application. In the sample project, DTOs are used as parameters for controller methods (or actions) in the WebApi. In other words, JSON is posted to those methods and with some MVC magic, the data is automatically converted to DTOs before being passed to the methods. Do you think it is wrong to use DTOs in this case. Should ViewModels be used with a Web API? I am asking to better understand, because I am still not all that familiar with these concepts.Aitchbone
Salut Jean-François Beauchamp :) ASP.NET MVC can parses url prams into an object, for example : suppose i have this mapping to an Index method ajax/index/{jobID}/{ResultsToSkip}/{ResultsToSend}" instead of having in the controlle Index(int jobID, int ResultsToSkip, int ResultsToSend) I will have Index(request) (request is an object that encapsulate 3 fields jobID ...) So now instead of params you are talking to your appication with objects that encapsulate DATA, so yes we can say requestDTO. For example you have to add one other field you change only the DTO, not the api interface methods.Corotto
P
3

If you need to change or enhance the DTO then create a ViewModel. It's also OK for the ViewModel to reference the DTO as a complex property.

In practice, you will generally want to add view-specific properties or methods to the model you are using in the view. In such cases, never modify the DTO for your view requirements. Instead, create a ViewModel and map from your DTO to the ViewModel.

Parsnip answered 12/1, 2021 at 7:43 Comment(0)
P
1

If you will use DTO as ViewModel, that means you are making high dependency on DTO because of some reason you are changing DTO then it could impact on ViewModel.

Better use DTO & convert into viewmodel.

Poltergeist answered 8/2, 2016 at 16:41 Comment(0)
G
0

In professional terms, a DTO (Data Transfer Object) and a ViewModel serve similar purposes but have distinct differences.

A DTO is an object used to transfer data between different layers or components of an application, typically between the backend and frontend. It focuses on data encapsulation and typically carries only the necessary data required by the receiving component. Its main objective is to optimize data transfer and minimize network calls.

On the other hand, a ViewModel is a design pattern commonly used in user interface development, specifically in the context of Model-View-ViewModel (MVVM) architecture. It acts as an intermediary between the view (user interface) and the model (data and business logic). The ViewModel provides data and behavior necessary for the view to display and interact with the underlying model.

While a DTO is primarily concerned with data transfer and encapsulation, a ViewModel is focused on the presentation and interaction aspects of a user interface. Although they may share similarities in terms of data representation, their purposes and contexts differ.

Garibull answered 2/11, 2019 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.