Back in the question about DTO has already got a good answer about what he is, except for the fact that speak in fields as if they were attributes, but it is a mistake that 99.99% of people make even :).
Let’s forget a little, and just for a moment, the terminology used in MVC not to confuse. Let’s use the database terminology that is most common people understand.
DTO is a way of organizing information in the application that comes from some source(s), usually a database, but may be another mechanism. We can understand the DTO as a view that you already know from the database. Not that it is exactly this, but just to understand. The DTO takes data from the data source and assembles an easy-to-use object used by the application. In general DTO makes a simple mapping and has no behavior. It has direct relationship to the data source, and can be used in various parts of the application only for consumption.
Usually when this type of object has behavior it happens to be a pattern called Active Record. In some cases Domain Object or other terminology is used with subtle differences.
DTO takes data from a source, probably a table from a database, and assembles a simple object. The AR allows to have a logic of how the transfer will be made, and even business logic in it, although some authors indicate that there is already something else.
DTO is a mechanism that can be used as part of the model of MVC, but it is usually the AR that is used.
I don’t like the question about DTO and view model, I think you’re confused, probably wrong and follow a specific line of development that I don’t like. I’ll ignore it.
Continuing in the analogy with the database view model, it’s like a view of a database you already know, but only within your application. It is a model for the view of MVC. It is not a model for the database. Of course he will have to communicate with the database model to get data, but the consumption is from view of the MVC.
There are many simple cases that using an object via DTO, AR, DO, or whatever is suitable and simple, there is an easy relationship between that object and what it will use in view of MVC. But there are cases that are not so simple, you need to object that will be consumed by view that it is more complex that it is composed of a subset of fields of those objects that bind to the database, or fields created only on those objects because the application’s database or objects do not provide them, or it still needs data that is in several objects and it becomes complicated to use them separately, so compile (in the sense of joining, not analyzing a source code :) all this into an object that fits the demand of the view of the MVC. That is to say view model maps to the view of the MVC, while the model maps with the database.
Then the view model it’s like a SELECT
pre-established that generates a ready-to-use object in the view.
There is no relationship between the two. Thinking that the DTO is a mechanism of view model, in my view, it is wrong, but it is possible to make this analogy as a DTO between the MVC model and the MVC view.
If you don’t have a view in play, there’s no point in using a view model. And view may be from a design pattern known as the MVC, MVP, or MVVM, and probably in RUI (which I’m still studying, and looks good), but may be views outside of these known patterns, there are people who use an M(Odel)V(iew) and a model view may be useful even in such cases.
DTO is a primitive mechanism. Alone, in its essence, it is useful in simple and direct applications. It is almost impossible to have a database application that does not have some form of DTO or its derivation, usually an AR, even if it does not seem, since there are languages and frameworks that abstract it for you. The viewmodel is not necessary unless the mechanism that communicates with the view demand, which is common in frameworks MVC out there, but it’s mostly their disability.
For it is clear, the model of MVC is always a form, almost always more sophisticated, of DTO, in general even more sophisticated than an AR. Often the model is represented by a repository, which is full of Ars and other mechanisms.
Good +1, I’ve been researching about, but for me it didn’t make much sense when using one and the other, I’ll wait for an answer here!
– Marconi
I had already searched for a while, but also had not understood. Usually I use Viewmodel in Web and DTO in Apis. but because I learned so, without a why(which is terrible)
– Barbetta