Java data transfer object naming convention?

Given this scenario, when you have “transfer objects” (POJOs using getters / setters only) that are passed by the client library to your API, what is the best way to name transfer objects?

package com.x.core; public class Car { private String make; private String model; public Car(com.x.clientapi.Car car) { this.make = car.getMake(); this.model = car.getModel(); } } 

In this example, your main class and your transfer object are named Car . They are in different packages, but I think it confuses one name. Is there a best practice on how to name transfer objects?

+43
java naming-conventions
Nov 12 '09 at 19:30
source share
5 answers

I usually add 'DTO' at the end of the class name and also put all the DTOs in my own package. In your example, I would call it com.x.core.dto.CarDTO.

+32
Nov 12 '09 at 19:36
source share

D ata T translation O bject classes must conform to the naming convention defined in the Java language specification :

Class type names must be descriptive nouns or nominal phrases, not too long, in mixed case, with the first letter of each word capitalized.

 ClassLoader SecurityManager Thread Dictionary BufferedInputStream 

[...]




A class name suffix using DTO or Dto does not make much sense and says little about the class itself. Try using names that describe the purpose of your studies.

Here is a partial list of name suggestions you can use:

  • SomeSortOf Command
  • SomeSortOf Configuration
  • SomeSortOf Permissions
  • SomeSortOf Details
  • SomeSortOf element
  • SomeSortOf Event
  • EventsSomeSortOf Header
  • SomeSortOf Input
  • SomeSortOf Instruction
  • SomeSortOf Item
  • SomeSortOf Message
  • PostSomeSortOf Metadata
  • SomeSortOf Operation
  • SomeSortOf Exit
  • SomeSortOf Payload
  • SomeSortOf Projection
  • ProjectionSomeSortOf QueryParameter
  • SomeSortOf QueryResult
  • SomeSortOf View
  • SomeSortOf Request
  • SomeSortOf Resources
  • SomeSortOf Response
  • AnswerSomeSortOf Result
  • SomeSortOf Series
  • SomeSortOf Settings
  • SomeSortOf Specification
  • SomeSortOf Status
  • SomeSortOf Summary



Note 1: Whether acronyms or all capitalized words should be handled as words or not, I guess it up to you. Check the Java API and you will find some stumbles like [TG41] / [TG42] . Both classes are in the same package and the name convention is not consistent. [TG43] does not 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 a cached copy from Web Archive).

+151
Feb 11 '16 at 14:14
source share

Adding DTO or DAO or anything else violates DRY. FQN is great, especially if they are really the same.

+5
Nov 12 '09 at 19:50
source share

I don’t think there is a best practice or convention for a class that demonstrates this behavior. I personally don't like the word Object in any of the class names. You can either use some qualification, like Poko.Car, or use some kind of naming convention, for example Car (for POJO) CarDa (for data access) CarBiz (for business domain class)

Or if you don't mind the word object in the class name, go on to something like CarDto (car data transfer object)

+2
Nov 12 '09 at 19:37
source share

Use a convention suitable for other code conventions. I personally use the suffix "TO" (for example, the data transfer object associated with the Customer domain class is called CustomerTO). Also, the package structure should convey the intent of each type of class (so.foo.domain.Customer and so.foo.transport.CustomerTO)

-3
Nov 12 '09 at 19:40
source share



All Articles