Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can DTO have basic object level operations like Clone?

Tags:

c#

dto

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.

like image 242
cng Avatar asked Sep 01 '11 18:09

cng


People also ask

Can DTO have methods?

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.

What is difference between DTO and model?

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.

Can a DTO contain another DTO?

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.

What is the difference between DTO and entity?

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.


1 Answers

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.

like image 105
Marc Gravell Avatar answered Sep 27 '22 21:09

Marc Gravell