Data transfer object
In the field of programming a data transfer object (DTO[1][2]) is an object that carries data between processes. The motivation for its use is that communication between processes is usually done resorting to remote interfaces (e.g., web services), where each call is an expensive operation.[2] Because the majority of the cost of each call is related to the round-trip time between the client and the server, one way of reducing the number of calls is to use an object (the DTO) that aggregates the data that would have been transferred by the several calls, but that is served by one call only.[2]
The difference between data transfer objects and business objects or data access objects is that a DTO does not have any behavior except for storage, retrieval, serialization and deserialization of its own data (mutators, accessors, serializers and parsers). In other words, DTOs are simple objects that should not contain any business logic but may contain serialization and deserialization mechanisms for transferring data over the wire.[1]
This pattern is often incorrectly used outside of remote interfaces. This has triggered a response from its author[3] where he reiterates that the whole purpose of DTOs is to shift data in expensive remote calls.
In some languages, these can usually be represented as records (or structs).
Terminology
A value object is not a DTO. The two terms have been conflated by Sun/Java community in the past.[2]
For the model–view–viewmodel pattern, the data transfer object can be referred to as the viewmodel.[4][dubious – discuss]
Example
The following is an example of a DTO, using a Java record.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
record UserResponseDTO(
Long id,
@NotBlank(message = "Name cannot be empty") String name,
@Email(message = "Invalid email format") String email
) {}
try (FileInputStream f = new FileInputStream("config.properties") {
Properties prop = new Properties();
prop.load(f);
UserResponseDTO user = new UserResponseDTO(1L, prop.getProperty("user.name"), prop.getProperty("user.email"));
System.out.println(user.name());
} catch (IOException e) {
System.err.println("Error loading properties file: " + e.getMessage());
}
References
- ^ a b MSDN (2010). Data Transfer Object. Microsoft MSDN Library. Retrieved from https://msdn.microsoft.com/en-us/library/ms978717.aspx.
- ^ a b c d Fowler, Martin (2010). Data Transfer Object. Patterns of Enterprise Application Architecture. Retrieved from http://martinfowler.com/eaaCatalog/dataTransferObject.html.
- ^ LocalDTO. Retrieved from http://martinfowler.com/bliki/LocalDTO.html.
- ^ Microsoft Learn (3 January 2024). "Tutorial: Create a web API with ASP.NET Core".
External links
- Summary from Fowler's book
- Data Transfer Object - Microsoft MSDN Library
- GeDA - generic dto assembler is an open source Java framework for enterprise level solutions
- Local DTO
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.