Java data transfer object naming convention?
I generally add 'DTO' to the end of the Class name as well as place all the DTO's in their own package. In your example I would call it com.x.core.dto.CarDTO.
I read the answers above, I just want to add something. I somehow hate the word DTO
, It seems like it is screaming at me. So I try to use Payload
suffix. For example, CarPayload
.
Data Transfer Object classes should follow the name convention defined in the Java Language Specification:
Names of class types should be descriptive nouns or noun phrases, not overly long, in mixed case with the first letter of each word capitalized.
ClassLoader SecurityManager Thread Dictionary BufferedInputStream
[...]
Suffixing a class name with DTO or Dto won't tell much about the class itself besides indicating it carries data without any behaviour. So, instead of just calling your objects DTO, it might be worth considering more meaningful names, which convey better semantics for the classes.
Here is a non-exhaustive list of name suggestions you could use:
SomeSortOfCommand
SomeSortOfConfiguration
SomeSortOfCredentials
SomeSortOfDetails
SomeSortOfElement
SomeSortOfEvent
SomeSortOfFilter
SomeSortOfHeader
SomeSortOfInput
SomeSortOfInstruction
SomeSortOfItem
SomeSortOfMessage
SomeSortOfMetadata
SomeSortOfOperation
SomeSortOfOutput
SomeSortOfPayload
SomeSortOfProjection
SomeSortOfProperties
SomeSortOfQueryParameter
SomeSortOfQueryResult
SomeSortOfRepresentation
SomeSortOfRequest
SomeSortOfResource
SomeSortOfResponse
SomeSortOfResult
SomeSortOfRow
SomeSortOfSettings
SomeSortOfSpecification
SomeSortOfStatus
SomeSortOfSummary
Note 1: Whether acronyms or all capitalized words should be handled as words or not, I guess it's up to you. Check the Java API and you will find some stumbles like ZipInputStream
/ GZIPInputStream
. Both classes are in the same package and the name convention is not consistent. HttpURLConnection
doesn't show any consistency with acronyms either.
Note 2: Some names listed above were borrowed from this article written by Richard Dingwall (the original article seems to be no longer available, so here's a cached copy from Web Archive).
Adding DTO or DAO or anything else violates DRY. The FQN is perfectly fine, especially if they're really the same thing.