views:

2608

answers:

4

what is a Data Transfer Object?

In MVC are the model classes DTO, and if not what are the differences and do we need both?

+1  A: 

With MVC data transfer objects are often used to map domain models to simpler objects that will ultimately get displayed by the view.

From Wikipedia:

Data transfer object (DTO), formerly known as value objects or VO, is a design pattern used to transfer data between software application subsystems. DTOs are often used in conjunction with data access objects to retrieve data from a database.

Dan
+4  A: 

The definition for DTO can be found on Martin Fowler's site. DTOs are used to transfer parameters to methods and as return types. A lot of people use those in the UI, but others inflate domain objects from them.

blu
+1  A: 

A DTO is a dumb object - it just holds properties and has getters and setters, but no other logic of any significance (other than maybe a compare() or equals() implementation).

Typically model classes in MVC (assuming .net MVC here) are DTOs, or collections/aggregates of DTOs

Eric Petroelje
+1  A: 

A Data Transfer Object is an object that is used to encapsulate data, and send it from one subsystem of an application to another.

DTOs are most commonly used by the Services layer in an N-Tier application to transfer data between itself and the UI layer. The main benefit here is that it reduces the amount of data that needs to be sent across the wire in distributed applications. They also make great models in the MVC pattern.

Another use for DTOs can be to encapsulate parameters for method calls. This can be useful if a method takes more than 4 or 5 parameters.

When using the DTO pattern, you would also make use of DTO assemblers. The assemblers are used to create DTOs from Domain Objects, and vice versa.

The conversion from Domain Object to DTO and back again can be a costly process. If you're not creating a distributed application, you probably won't see any great benefits from the pattern, as Martin Fowler explains here

Benny Hallett
"DTO make great models in the MVC pattern" - but shouldn't a model contain all data of the object and DTO be optimized with part of the data?If I have model A and I need to pass it to two subsystems will there be A_DTO_1 and A_DTO_2 with the relevant fields of each?"DTOs can be to encapsulate parameters for method calls" --> So every class that wraps parameters is DTO even if this is not distributed system?Are't models in MVC the domain object?
Yaron Naveh
In answer to your first question, I don't think were talking about the same thing. The model in MVC doesn't necessarily need to be a class from your Domain Model. Having said that, it well could be. Using the DTO strips out all of the unnecessary stuff. Just depends on the architecture you're going for. I'm not sure exactly how to answer your second question. Whether its across the wire or not, it's still an object that encapsualtes a bunch of data to be transferred between (sub)systems, so I'd argue it's a DTO.
Benny Hallett