I understand DTO's strict definition is to serve as container to transport data, it shouldn't have any behavior. However, I have faced a situation with a need to clone the DTO, two options: 1. create a Clone method (ICloneable?) in DTO 2. create generic utility class to clone DTO
I currently use option #2. However, I think #1 is acceptable provided there are no logic in DTO. I would like see if any of you faced a similar situation with DTO that required basic operations like Clone, ToString, especially DTOs that had inheritance. Thanks.
DTO doesn't have methods. All of the methods that do calculations based on members of DTO are in the dependent class. The more DTOs the dependent class depends on, the more private methods the class has. The more private methods the class has, the harder it is to understand code and write unit tests.
Do you know the difference between data transfer objects and view models? Data Transfer Objects (DTOs) and View Models (VMs) are not the same concept! The main difference is that while VMs can encapsulate behaviour, DTOs do not. The purpose of a DTO is the transfer of data from one part of an application to another.
public – Only public classes with public members work as DTO. static – Fields must not be static but inner classes must be static. no-arg constructor – To allow instantiation before setting the fields. extend – DTOs can extend another DTO.
Difference between DTO & Entity: Entity is class mapped to table. Dto is class mapped to "view" layer mostly. What needed to store is entity & which needed to 'show' on web page is DTO.
If it is a DTO, it should be designed for serialization - in which case your best option is to serialize it via whatever process it is designed, and rehydrate a clone from there. It is pretty rare that this would be a performance issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With