实体与DTO之间的区别 [英] Difference between Entity and DTO

查看:1375
本文介绍了实体与DTO之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DTO和实体之间有什么区别?详细地,这些是我的问题:

What is the difference between a DTO and an Entity? In details these are my questions:


  1. DTO应该具有哪些字段?例如,我的实体类为:

  1. What fields should the DTOs have? For example my entity classes are:

@Entity
public class MyFirstEntity implements Serializable {

    @Id @GeneratedValue
    private Long id;

    private String stringData;

    @OneToOne
    private MySecondEntity mySecondEntity;

    @OneToMany
    private List<MySecondEntity> mySecondEntitesList;

}

@Entity
public class MySecondEntity implements Serializable {

    @Id @GeneratedValue
    private Long id;

    private Integer integerData;

    @ManyToOne
    private MyFirstEntity myFirstEntity;

}


有一个单面连接(一对一)和一个两面连接(多对一),一个简单的String和Integer数据,当然还有id。在 MyFirstDTO MySecondDTO 类中要从中添加什么?

There is a one-sided connection (One-to-one) and a two-sided connection (Many-to-one), a simple String and Integer data and of course the ids. What to put from them in the MyFirstDTO and MySecondDTO classes?


  1. 如果实体之间存在继承关系,那么我该如何在DTO中表示它?例如:

  1. If there is an inheritance between the entities, then how should I represent it in the DTOs? For example:

@Entity
public class MyFirstEntity extends MySecondEntity {
    ....
}

@Entity
public class MyFirstDTO extends MySecondDTO {
    ....
}


  • 我应该如何使用它们?例如,我发现了这一点:我正在开发一个Web项目。网页的用户想要注册。他/她填写表格,并将其发送到服务器。在服务器端,我首先创建一个DTO,因为其字段具有验证。从DTO中,我创建一个实体并将其持久保存到数据库中。当有实体请求时,我将所请求的实体转换为DTO,并将其提供给客户端的用户。

  • How should I use them? For example, I find out this: I'm working on a web project. The user of the webpage wants to register. He/She fills the forms, and sends it to the server. On the server side I create first a DTO, because its fields have the validations. From the DTO I create an Entity and persist it to the database. When there is a request for an entity, I convert the requested entity to DTO, and give it to the user on the client side. Is it a good imagination, or not?


    推荐答案

    简短答案:


    • 实体可能是商业域的一部分。因此,它们可以实现行为并将其应用于域内的不同用例。

    • Entities may be part of a business domain. Thus, they can implement behavior and be applied to different use cases within the domain.

    DTO仅用于将数据从一个过程或上下文传输到另一个过程或上下文。因此,它们没有行为-除了非常基本且通常是标准化的存储和检索功能。

    DTOs are used only to transfer data from one process or context to another. As such, they are without behavior - except for very basic and usually standardised storage and retrieval functions.

    长答案:

    虽然术语数据传输对象(DTO)的定义很明确,但是对
    术语实体的解释却有所不同在我看来,

    While the term "Data Transfer Object" (DTO) is defined quite unambiguously, the term "Entity" is interpreted differently in various contexts.

    与实体一词最相关的解释是以下三种:

    The most relevant interpretations of the term "Entity", in my opinion, are the following three:


    1. 在企业级Java和jpa的上下文中:

      表示数据库中维护的持久性数据的对象。

    1. In the context of enterprise java and jpa:
      "An object that represents persistent data maintained in a database."

    在域驱动的设计(由Eric Evans撰写)中:

    主要由其身份而不是其属性定义的对象。

    In the context of "domain-driven-design" (by Eric Evans):
    "An object defined primarily by its identity, rather than its attributes."

    在干净的体系结构中(由Robert C. Martin撰写) :

    封装企业范围内关键业务规则的对象。

    In the context of "clean architecture" (by Robert C. Martin):
    "An object that encapsulates enterprise-wide critical business rules."



    < p > Jee和Jpa社区主要将实体视为映射到数据库表
    的对象。这种观点与DTO
    的定义非常接近-这可能是造成混乱的原因。

    The Jee- and Jpa-community sees entities primarily as objects mapped to a database table. This point of view is very close to the definition of a DTO - and that's where much of the confusion probably stems from.

    在域-驱动设计以及Robert Martins的观点,
    然而,实体是业务领域的一部分,因此
    可以并且应该实现行为。

    In the context of domain-driven-design, as well as Robert Martins point of view, however, Entities are part of a business domain and thus can and should implement behavior.

    这篇关于实体与DTO之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆