Transfer 对象和 Domain 对象之间的区别

发布于 2024-11-24 05:25:10 字数 70 浏览 1 评论 0原文

您能简单地解释一下 Transfer 对象和 Domain 对象之间的区别吗?如果你能给出一个 Java 例子,那就太好了..

Could you please explain the difference between Transfer objects and Domain objects in simple terms ? And if u could give a Java example, that would be great..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云醉月微眠 2024-12-01 05:25:10
  • DTO 没有任何逻辑。他们只有字段(状态)。当将数据从一层/子系统传输到另一层时使用它们
  • 域对象可以具有逻辑(取决于您是否使用域驱动设计或具有贫乏的数据模型)并且它们通常与数据库结构相关。

如果使用贫乏的数据模型(即您的域对象没有任何逻辑),DTO 和域对象可以是同一个对象。

相关:http://techblog.bozho.net/?p=427

  • DTOs don't have any logic. They only have fields (state). They are used when transferring data from one layer/subsystem to another
  • Domain objects can have logic (depending on whether you are using domain-driven design or have anemic data model) and they are usually related to the database structure.

If using anemic data model (i.e. your domain objects don't have any logic), DTO and domain object can be the same object.

Related: http://techblog.bozho.net/?p=427

云醉月微眠 2024-12-01 05:25:10

数据传输对象(DTO)用于在应用程序的不同部分(例如不同层)或不同应用程序之间交换数据。

  • DTO 只是一种“哑”数据结构。
  • 它们用在契约/接口定义中——这意味着使用这些接口之一的任何组件都“知道”这些对象。

域对象(DO)(以及派生它们的类)实现业务逻辑,因此它们仅位于业务逻辑层/域中(即使术语是不同的)。

因为 DO 实现业务逻辑,所以它们可能很复杂,并且可以包括方法、事件等。

关于 DTO 的另一点

根据 Martin Fowler 学派的观点,DTO 是多个对象的组合(每个对象都是大多数人通常所说的 DTO);其基本原理是,在某些情况下,以较低的频率发送较大的数据包会更便宜(而不是“喋喋不休”并通过网络不断发送许多小数据包)。

因此,大多数人将 DTO 视为单个对象,Martin F 表示 DTO 只是一个包含几个离散(可能不相关)对象的“信封”。在我看来,这不是一个大问题 - 大多数人认为 DTO 是按照我在这个答案顶部的定义(或接近它的东西)。

A Data-Transfer-Object (DTO) is used to exchange data between different parts of an application (such as different layers), or different applications.

  • DTO's are simply a "dumb" data structure.
  • They are used in contracts / interface definitions - this means that any component that uses one of these interfaces "knows" about these objects.

Domain Objects (DO) (and the classes from which they are derived) implement business logic, as such they are only located in the Business logic layer / Domain (the essential meaning is the same even if the terms are different).

Because DO's implement business logic they can be complex, and can include methods, events and so on.

One more point about DTOs

According to the Martin Fowler school of thought a DTO is a combination of several objects (each of which would be what most people would commonly call a DTO); the rationale is that in situations it's less expensive to send larger packages of data less frequently (as opposed to being "chatty" and sending many small packages constantly across the wire).

So where most people would view a DTO as a single object Martin F is saying a DTO is simply an "envelop" that contains several discrete (and possibly unrelated) objects. Not a big issue IMO - most people take the view that a DTO is as per my definition at the top of this answer (or something close to it).

箹锭⒈辈孓 2024-12-01 05:25:10

由于其使用的性质,传输对象通常是可序列化的,如果调用是远程的并且在 JVM 之间,或者它们将以促进序列化的方式使用(例如有状态会话 Bean),则这一点尤其相关。为此,传输对象必须易于接受这种“处理”,即传输对象实现可序列化。

领域对象则不然。领域对象可能包含行为,但 DTO 只是一种传输介质。

Transfer objects are often serializable due to the nature of its use, this is especially pertinent if the calls are remote and between JVM, or they will be used in a way that promotes serialization, such as stateful session beans. For this purpose, transfer objects must be susceptible to such "treatment", I.e. Transfer objects implement serializable.

The reverse may not be said of domain objects. Domain objects may contain behavior, however DTO are merely a transportation medium.

贱人配狗天长地久 2024-12-01 05:25:10

业务角度的领域对象,它是整个领域和流程的活动的参与者或主体,DTO是数据对象,它与忠实反映数据库对象的数据bean不同(尤其是在ORM架构中)
DTO可以进行数据聚合、转换、组合以满足表示或某些数据操作的需要,它没有主题,但域对象根据其域级别定义有主题

Domain object in business perspective, it is actor or subject of the activity with whole domain and flows, DTO is the data object which is not the same as data bean that faithfully to reflect database objects (especially at ORM architecture)
DTO can has data aggregation, transform , combination to satisfy the representation or some data munipulate needs , it is no subject , but Domain object has subject base on their domain level defination

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文