Web 服务中 DTO 的命名约定是什么
What is naming convention for DTOs in a webservice
我正在设计一个 restful Web 服务,我想知道我应该如何命名我的 DTO。我可以为它们使用像 Request 和 Response 这样的后缀吗?例如对于 addUser 服务,将有 2 个 DTO 命名为:AddUserRequest 和 AddUserResponse。
DTO(数据传输对象)就像 POJO(普通旧 Java 对象)。它应该只有 getters
和 setters
而不是任何业务逻辑。
来自维基百科:
A data transfer object is an object that carries data between
processes. The motivation for its use is that communication between
processes is usually done resorting to remote interfaces (e.g., web
services), where each call is an expensive operation. Because the
majority of the cost of each call is related to the round-trip time
between the client and the server, one way of reducing the number of
calls is to use an object (the DTO) that aggregates the data that
would have been transferred by the several calls, but that is served
by one call only.
The difference between data transfer objects and business objects or
data access objects is that a DTO does not have any behavior except
for storage and retrieval of its own data (mutators and accessors).
DTOs are simple objects that should not contain any business logic
that would require testing.
This pattern is often incorrectly used outside of remote interfaces.
This has triggered a response from its author[3] where he reiterates
that the whole purpose of DTOs is to shift data in expensive remote
calls.
因此,理想情况下,您应该为这些操作创建一些助手,或者您可以将它们添加为控制器。
您的组织是否已有描述您传入的规范用户的模式?如果那是您正在使用的名称,那么您当然会使用该架构中的名称。否则,就像描述任何 class 或模式元素一样描述它们。
请注意,由于 DTO 不包含自己的方法,因此您可能不会为其命名并带有动作动词。
但是,考虑将它们称为 AddUserRequest 和 AddUserResponse,尤其是当该方法需要比普通用户 DTO 更多的信息时。这符合接口隔离原则,因为你的接口参数应该专门为请求本身量身定制(它不应该需要与请求无关的元素;你不应该有改变请求的函数类型参数,那些应该被提取到他们自己的调用中。) AddUserRequest 然后可能包含一个名为 User 的元素,它保存用户特定的数据,另一个元素保存请求的其他相关数据集,可能是组或访问权限,诸如此类.
因为它是一个 RESTful 服务,理想情况下,用户 addition/creation 请求应该发回 201 created HTTP status code ,userId 在位置 header 并且没有响应 body.对于请求,您可以将其命名为 UserDetails 或 UserData 或简称为 User。参考 https://pontus.ullgren.com/view/Return_Location_header_after_resource_creation
我正在设计一个 restful Web 服务,我想知道我应该如何命名我的 DTO。我可以为它们使用像 Request 和 Response 这样的后缀吗?例如对于 addUser 服务,将有 2 个 DTO 命名为:AddUserRequest 和 AddUserResponse。
DTO(数据传输对象)就像 POJO(普通旧 Java 对象)。它应该只有 getters
和 setters
而不是任何业务逻辑。
来自维基百科:
A data transfer object is an object that carries data between processes. The motivation for its use is that communication between processes is usually done resorting to remote interfaces (e.g., web services), where each call is an expensive operation. Because the majority of the cost of each call is related to the round-trip time between the client and the server, one way of reducing the number of calls is to use an object (the DTO) that aggregates the data that would have been transferred by the several calls, but that is served by one call only.
The difference between data transfer objects and business objects or data access objects is that a DTO does not have any behavior except for storage and retrieval of its own data (mutators and accessors). DTOs are simple objects that should not contain any business logic that would require testing.
This pattern is often incorrectly used outside of remote interfaces. This has triggered a response from its author[3] where he reiterates that the whole purpose of DTOs is to shift data in expensive remote calls.
因此,理想情况下,您应该为这些操作创建一些助手,或者您可以将它们添加为控制器。
您的组织是否已有描述您传入的规范用户的模式?如果那是您正在使用的名称,那么您当然会使用该架构中的名称。否则,就像描述任何 class 或模式元素一样描述它们。
请注意,由于 DTO 不包含自己的方法,因此您可能不会为其命名并带有动作动词。
但是,考虑将它们称为 AddUserRequest 和 AddUserResponse,尤其是当该方法需要比普通用户 DTO 更多的信息时。这符合接口隔离原则,因为你的接口参数应该专门为请求本身量身定制(它不应该需要与请求无关的元素;你不应该有改变请求的函数类型参数,那些应该被提取到他们自己的调用中。) AddUserRequest 然后可能包含一个名为 User 的元素,它保存用户特定的数据,另一个元素保存请求的其他相关数据集,可能是组或访问权限,诸如此类.
因为它是一个 RESTful 服务,理想情况下,用户 addition/creation 请求应该发回 201 created HTTP status code ,userId 在位置 header 并且没有响应 body.对于请求,您可以将其命名为 UserDetails 或 UserData 或简称为 User。参考 https://pontus.ullgren.com/view/Return_Location_header_after_resource_creation