MVC 模型对象、域对象和 DTO 之间有什么区别 [英] What is the difference between an MVC Model object, a domain object and a DTO

查看:27
本文介绍了MVC 模型对象、域对象和 DTO 之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC 模型对象、域对象和 DTO 之间有什么区别?

What is the difference between a MVC Model object, a domain object and a DTO?

我的理解是:

MVC 模型对象:

对要由相应视图显示的数据进行建模.它可能不会直接映射到域对象,即可能包含来自一个或多个域对象的数据.

Models the data to be displayed by a corresponding view. It may not map directly to a domain object, i.e. may include data from one or more domain objects.

  1. 客户端
  2. 可能包含业务逻辑.例如.验证、计算属性等
  3. 没有与持久化相关的方法

域对象:

对问题域中的真实世界对象(如预订、客户、订单等)建模的对象.用于持久化数据.

An object that models real-world object in the problem domain like Reservation, Customer, Order, etc. Used to persist data.

  1. 服务器端
  2. 没有业务逻辑

DTO(数据传输对象):

当层处于不同的进程中时,用于在层之间传输数据的对象,例如从数据库到客户端应用程序.在获取与多个域对象对应的数据时,允许跨线路进行单个事务而不是多个调用.DTO 只包含数据和访问器方法,不存在逻辑.数据用于特定的 DB 事务,因此它可能会也可能不会直接映射到域对象,因为它可能包含来自一个或多个域对象的数据.

An object used to transfer data between layers when the layers are in separate processes, e.g. from a DB to a client app. Allows a single transaction across the wire rather than multiple calls when fetching data corresponding to multiple domain objects. A DTO contains just data and accessor methods and there is no logic present. The data is for a particular DB transaction, so it may or may not directly map to a domain object as it may include data from one or more domain objects.

  1. 在服务器端和客户端使用,因为它在层之间传递
  2. 没有业务逻辑
  3. 没有与持久化相关的方法

那么,问题:

  1. 以上理解正确吗?我是否遗漏了一些关键点?

  1. Is above understanding correct? Am I missing some key points?

假设模型对象不需要额外的业务逻辑,是否有任何理由不使用域对象作为 MVC 模型?

Are there any reasons not to use Domain objects as the MVC Model assuming that the Model objects do not require extra business logic?

假设模型对象不需要额外的业务逻辑,是否有任何理由不使用 DTO 作为 MVC 模型?

Are there any reasons not to use DTOs as the MVC Model assuming that the Model objects do not require extra business logic?

推荐答案

域和模型对象本质上是相同的,并且可能包含业务逻辑.如果您将业务逻辑从模型中移除到服务类中,则域和 DTO 对象可能等效,具体取决于实现.

Domain and model objects are essentially the same, and may contain business logic. Depending on implementation, domain and DTO objects may be equivalent if you remove business logic from the model into a service class.

通常 DTO 的一个关键变体是视图模型,它纯粹用于在域模型和视图之间传输数据,尽管视图模型通常可能包含逻辑,尽管这应该是纯粹的 UI 逻辑.

Often a key variant of the DTO is the View Model, which is used purely to transfer data between the domain model and the view, although often a View Model may contain logic, although this should be purely UI logic.

这篇关于MVC 模型对象、域对象和 DTO 之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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