MVC 模型对象、域对象和 DTO 之间有什么区别
MVC 模型对象、域对象和 DTO 之间有什么区别?
我的理解是:
MVC Model对象:
对相应视图要显示的数据进行建模。它可能不直接映射到域对象,即可能包括来自一个或多个域对象的数据。
- 客户端
- 可能包含业务逻辑。例如。验证、计算属性等
- 没有与持久性相关的方法
域对象:
对问题域中的真实对象(如预订、客户、订单等)进行建模的对象。用于持久化数据。
- 服务器端
- 无业务逻辑
DTO(数据传输对象):
当层处于单独进程中时,用于在层之间传输数据的对象,例如从数据库到客户端应用程序。在获取与多个域对象对应的数据时,允许跨线路进行单个事务,而不是多个调用。 DTO 仅包含数据和访问器方法,不存在任何逻辑。该数据用于特定的数据库事务,因此它可能会或可能不会直接映射到域对象,因为它可能包括来自一个或多个域对象的数据。
- 在服务器端和客户端上使用,因为它在层之间传递
- 没有业务逻辑
- 没有与持久性相关的方法
那么,问题是:
上述理解正确吗?我是否遗漏了任何关键点?
假设模型对象不需要额外的业务逻辑,是否有任何理由不使用域对象作为 MVC 模型?
假设模型对象不需要额外的业务逻辑,是否有任何理由不使用 DTO 作为 MVC 模型?
What is the difference between a MVC Model object, a domain object, and a DTO?
My understanding is:
MVC Model object:
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.
- Client side
- May contain business logic. Eg. validations, calculated properties, etc
- No persistence related methods
Domain object:
An object that models real-world object in the problem domain like Reservation, Customer, Order, etc. Used to persist data.
- Server side
- No business logic
DTO (Data Transfer Object):
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.
- Used on both server and client sides as it is passed between layers
- No business logic
- No persistence related methods
So, the questions:
Is above understanding correct? Am I missing any key points?
Are there any reasons not to use Domain objects as the MVC Model assuming that the Model objects do not require extra business logic?
Are there any reasons not to use DTOs as the MVC Model assuming that the Model objects do not require extra business logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
域和模型对象本质上是相同的,并且可能包含业务逻辑。根据实现的不同,如果将业务逻辑从模型中删除到服务类中,域和 DTO 对象可能是等效的。
通常,DTO 的一个关键变体是视图模型,它纯粹用于在域模型和视图之间传输数据,尽管视图模型通常可能包含逻辑,尽管这应该是纯粹的 UI 逻辑。
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.
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 的区别)。
通常,一个应用程序(轻型应用程序)可以有一个模型 - 在这种情况下,您的模型正是您的领域。
另一种模型可以是完全不同的对象类型,即处理另一种对象类型。在这种情况下,它们都是您的域的一部分,并被命名为“域模型 - 对象”。
希望这个答案是详尽的并让您明白!
But the most interesting part is that, it does not have any behavior except for storage and retrieval of its own data!!!
Sticking with the MVC methodology...
They can obvioussly have behaviour and properties(see difference with DTO).
Often an application (a light one) can have one model - case in which your model is exactly your domain.
Another model can be, a totaly different object type, that is processing another one. Both of them, in this case are part of your domain, and are named "domain models - objects".
Hopefully this answer is exhaustive and makes it all clear for you !
我的理解(简而言之)如下:
(MVC)模型对象:
PersonEditModel
、PersonViewModel
或只是PersonModel
域对象:
DTO(数据传输对象):
My understanding (in a big short) is as follows:
(MVC) model object:
PersonEditModel
,PersonViewModel
or justPersonModel
Domain object:
DTO (Data Transfer Object):
域和 DTO 也可以是您的“模型”对象 - 您可以有一个视图来呈现“客户”域对象的详细信息。
域对象可以具有业务逻辑来强制执行域实体的属性。验证就是这样一种情况。域对象本身不包含与持久性相关的方法,但它可以具有元数据(如注释)来支持持久性
POJO 编程模型使得可以使用与域、DTO 和模型对象相同的对象 - 本质上,您不会实现任何仅适用于一层而不适用于其他层的无关接口。
The Domain and DTO can also be your "model" objects - you can have a view to render the details of the "Customer" domain object.
A domain object can have business logic to enforce the properties of the domain entity. validation is one such case. The domain object by itself does not contain persistence related methods, but it can have meta-data (like annotations) to support persistence
the POJO programming model makes it possible to use the same object as your domain, DTO and model objects - essentially, you will not be implemented any extraneous interfaces that will only apply to one layer but does not apply to others.
MVC 和 DDD 可以一起使用。我们在 DDD 和 MVC 中所说的“模型”实际上是相同的:抽象。我们可以使用伪代码来说明一些示例。
模型视图控制器 (MVC)
模型视图控制器架构将软件分为三个部分:
模型层
MVC 架构中的模型层是逻辑所在的位置。在这一层中,我们有我们的模型和业务逻辑。
一个简单的汽车抽象。
使用服务的汽车的简单 CRUD
视图层
视图层是用户界面驻留。用户可以在此处与系统交互,然后系统将触发所执行的操作的控制器,然后控制器将通知模型层并请求数据。视图层可以驻留在应用程序的客户端或应用程序的服务器端(即:JSF(Java Server Faces)作为服务器端,ReactJS 作为客户端)。不管怎样,即使View层驻留在客户端,客户端也需要请求服务器端发送请求。这可以通过基于 Web 应用程序的 HTTP 请求来完成。
汽车的伪页面。
控制器层
控制器层基本上接收来自视图的输入,然后转换数据并将其发送到模型层,反之亦然。
加载汽车的方法。
领域驱动设计(DDD)
领域驱动设计是一个概念:
DDD 的概念奠定了基础,即类、类变量和类方法必须与其核心业务领域相匹配。
领域驱动设计驻留在“M”中
在这种情况下,当应用 MVC 时,领域驱动设计设计驻留在 MVC 架构的模型层中。如前所述,模型层是应用程序的业务逻辑所在的位置。
无论您是否有实体,它们仍然是模型。模型只是现实世界中某些事物的抽象。如果抽象的话,猫可以是模型:
简单的猫抽象。 的模型。
它是 Cat. DDD 实体
在领域驱动设计中,我们有实体,它们也被归类为模型。它们之间的区别在于实体是可识别的。如果您有一个可识别且可以持久化的类,那么它就是一个实体。实体仍然是模型。
一个简单的实体。实体就是模型。
数据传输对象 (DTO)
数据传输对象内部没有逻辑,它们的唯一用途是作为将数据从一个端点传输到另一个端点的容器。通常企业实体本质上是不可序列化的,因此我们需要一种方法来仅发送需要发送给客户端的数据。
例如,由于模型可能具有敏感数据或我们不想在获取请求中共享的简单数据,那么考虑到我们的 Cat 模型,我们可以创建一个不共享 Cat ID 的 DTO:
A Cat 的数据传输对象。我们只需要它的属性以及获取和设置属性的东西。我们不想共享它的 ID。
因此,例如,如果我们必须使用 REST 从客户端请求所有猫的列表,那么我们将请求使用 CatDTO 而不是 Cat 进行响应的端点实体:
这就是我们的客户可以看到的所有数据。
MVC and DDD can be used together. What we call "Models" both in DDD and MVC are virtually the same: abstractions. Using pseudo-code we can illustrate a few examples.
Model View Controller (MVC)
The Model View Controller ARCHITECTURE separates the software into three parts:
The Model Layer
The Model layer from the MVC Architecture is where the logic resides. In this layer we have our models and business logic.
A simple Car abstraction.
A simple CRUD for Car using a Service
The View Layer
The View layer is where the user interface resides. Here's where the user can interact with the system, which will then trigger the Controllers on actions performed, which will then inform the Model layer and request data. The View Layer can reside either in the client-side of the application or the server-side of the application (ie: JSF (Java Server Faces) as Server-Side, ReactJS as Client-Side). By any means, even if the View layer resides on the client side, the client will need to request the server-side for sending requests. This may be done by HTTP requests for a Web-Based Application.
A pseudo-page for the Car.
The Controller Layer
The Controller layer basically receives input from the View and then convert and send the data to the Model Layer and vice-versa.
The method to load the Car.
Domain Driven Design (DDD)
Domain Driven Design is a concept:
DDD lays it's foundations in the concept that classes, class variables and class methods must match it's core business domain.
Domain Driven Design Resides into the "M"
In this case, when MVC is applied, the Domain Driven Design resides into the Model Layer of the MVC Architecture. As explained before, the Model Layer is where the Business Logic of the application resides.
Either if you have entities or not, they still are Models. A Model is just an abstraction of something in the real world. A cat can be a Model if abstracted:
Simple Cat abstraction. It is a Model of Cat.
DDD Entities
In Domain Driven Design we have Entities, that are also classified as Models. The difference between them is that Entities are identifiable. If you have a class that is identifiable and can be persisted, then it's an Entity. An Entity still, is a Model.
A simple Entity. An Entity is a Model.
Data Transfer Objects (DTO)
Data Transfer Objects have no logic inside them and the only use to them is to be containers for transferring data from one endpoint to another. Usually Enterprise Entities are not Serializable by nature, so we need a way to send only the data that we need to be sent to a client.
Since a Model could have sensible data or simply data we don't want to share in a request for a fetch, for instance, then considering our Cat Model, we could create a DTO that does not share the Cat's ID:
A Data Transfer Object for Cat. We only need it's properties and something to get and set the properties. We don't want to share it's ID.
So if we, for instance, had to request a list of all cats from our client using REST, then we would request the endpoint that responds with our CatDTO instead of our Cat Entity:
And that would be all the data that our client could see.
大多数对象的定义根据对象的使用位置而有所不同:
Any definition for most of the objects is various based on place of using of objects:
1)不,它是ViewModel的定义。 MVC 模型对象和域对象都是相同的。
2)领域模型(对象)始终存在,业务逻辑是可选的
3)如果Domain Object中没有业务逻辑,那么它自动成为DTO。
1) No, It is a definition of ViewModel. MVC Model Object and Domain Object both are same.
2) Domain Models (Objects) are always present, business logic is optional
3) If there is no business logic in Domain Object then automatically it becomes DTO.