Java数据传输对象命名约定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1724774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:54:41  来源:igfitidea点击:

Java data transfer object naming convention?

javanaming-conventionsdata-transfer-objects

提问by Marcus Leon

Given this scenario where you have "transfer objects" (POJO's with just getters/setters) which are passed by a client library to your API, what is the best way to name the transfer objects?

鉴于这种情况,您有“传输对象”(POJO 仅具有 getter/setter)由客户端库传递给您的 API,命名传输对象的最佳方法是什么?

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 both have the name Car. They are in different packages but I think it's confusing to have the same name. Is there a best practice on how to name the transfer objects?

在此示例中,您的主类和传输对象都具有名称Car. 它们在不同的包中,但我认为具有相同的名称会令人困惑。是否有关于如何命名传输对象的最佳实践?

采纳答案by IaCoder

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.

我通常在类名的末尾添加“DTO”,并将所有 DTO 放在它们自己的包中。在您的示例中,我将其称为 com.x.core.dto.CarDTO。

回答by Perpetualcoder

I dont think there is a best practice or convention for a class exhibiting this kind of behavior. I personally dont like the word Object in any of the class names. You could either use some qualification like Poko.Car or use some naming convention like Car (for POJO) CarDa (for data access) CarBiz ( for business domain class)

我认为对于表现出这种行为的班级来说,没有最佳实践或惯例。我个人不喜欢任何类名中的 Object 一词。您可以使用一些像 Poko.Car 这样的限定条件,也可以使用一些命名约定,如 Car(用于 POJO)CarDa(用于数据访问)CarBiz(用于业务领域类)

Or if you dont mind the word object in a class name go for something like CarDto (Car Data Transfer Object)

或者,如果您不介意类名中的单词 object,请使用 CarDto(汽车数据传输对象)之类的东西

回答by JuanZe

Use a convention that is suitable among the other code conventions you are using. I personally use the suffix "TO" (e.g. the data transfer object associated to the Customer domain class is named CustomerTO). Also the package structure should convey the intent of each type of class (so.foo.domain.Customer and so.foo.transport.CustomerTO)

使用适合您正在使用的其他代码约定的约定。我个人使用后缀“TO”(例如,与 Customer 域类关联的数据传输对象名为 CustomerTO)。此外,包结构应传达每种类的意图(so.foo.domain.Customer 和 so.foo.transport.CustomerTO)

回答by Jim Barrows

Adding DTO or DAO or anything else violates DRY. The FQN is perfectly fine, especially if they're really the same thing.

添加 DTO 或 DAO 或其他任何内容都违反了 DRY。FQN 非常好,特别是如果它们真的是一样的。

回答by cassiomolin

Data Transfer Objectclasses should follow the name conventiondefined in the Java Language Specification:

dATA牛逼转让(BOT)Øbject类应遵循命名约定在定义Java语言规范

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

[...]

类名应为描述性名词或名词短语,不宜过长,大小写混合,每个单词的首字母大写。

ClassLoader
SecurityManager
Thread
Dictionary
BufferedInputStream

[...]



Suffixing a class name with DTOor Dtois not really meaningful and doesn't tell much about the class itself. Consider using names that describe the purposeof your classes.

后面添加一个类名DTODTO是不是真的有意义的,并没有告诉太多关于类本身。考虑使用描述的名称目的的课程。

Here is a non-exhaustive list of name suggestions you could use:

以下是您可以使用的名称建议的非详尽列表:

  • SomeSortOfCommand
  • SomeSortOfConfiguration
  • SomeSortOfCredentials
  • SomeSortOfDetails
  • SomeSortOfElement
  • SomeSortOfEvent
  • SomeSortOfHeader
  • SomeSortOfInput
  • SomeSortOfInstruction
  • SomeSortOfItem
  • SomeSortOfMessage
  • SomeSortOfMetadata
  • SomeSortOfOperation
  • SomeSortOfOutput
  • SomeSortOfPayload
  • SomeSortOfProjection
  • SomeSortOfQueryParameter
  • SomeSortOfQueryResult
  • SomeSortOfRepresentation
  • SomeSortOfRequest
  • SomeSortOfResource
  • SomeSortOfResponse
  • SomeSortOfResult
  • SomeSortOfRow
  • SomeSortOfSettings
  • SomeSortOfSpecification
  • SomeSortOfStatus
  • SomeSortOfSummary
  • SomeSortOf命令
  • 某种配置
  • 某种凭证
  • 一些细节
  • SomeSortOf元素
  • SomeSortOf事件
  • SomeSortOf标题
  • 某种输入
  • 某种指令
  • 某种物品
  • 某种消息
  • 某种元数据
  • 某种操作
  • 某种输出
  • 某种有效载荷
  • 某种投影
  • SomeSortOf QueryParameter
  • SomeSortOf QueryResult
  • 某种表示
  • 某种请求
  • 某种资源
  • 某种反应
  • 某种结果
  • 某种类型的
  • 某种设置
  • 某种规格
  • 某种状态
  • 一些总结


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 APIand you will find some stumbles like ZipInputStream/ GZIPInputStream. Both classes are in the same packageand the name convention is not consistent. HttpURLConnectiondoesn't show any consistency with acronyms either.

注1:首字母缩略词或所有大写单词是否应作为单词处理,我想这取决于您。检查Java API,你会发现一些像ZipInputStream/ 的绊脚石GZIPInputStream。两个类都在同一个包中,命名约定不一致。HttpURLConnection也没有显示与首字母缩略词的任何一致性。

Note 2:Some names listed above were borrowed from this articlewritten by Richard Dingwall(the original article seems to be no longer available, so here's a cached copyfrom Web Archive).

注 2:上面列出的一些名称是从Richard Dingwall撰写的这篇文章中借用的(原始文章似乎不再可用,因此这里是Web Archive的缓存副本)。