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

发布于 2024-09-25 19:39:20 字数 797 浏览 3 评论 0原文

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

我的理解是:

MVC Model对象:

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

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

域对象

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

  1. 服务器端
  2. 无业务逻辑

DTO(数据传输对象)

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

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

那么,问题是:

  1. 上述理解正确吗?我是否遗漏了任何关键点?

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

  3. 假设模型对象不需要额外的业务逻辑,是否有任何理由不使用 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.

  1. Client side
  2. May contain business logic. Eg. validations, calculated properties, etc
  3. 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.

  1. Server side
  2. 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.

  1. Used on both server and client sides as it is passed between layers
  2. No business logic
  3. No persistence related methods

So, the questions:

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

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

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

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

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

发布评论

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

评论(7

尸血腥色 2024-10-02 19:39:20

域和模型对象本质上是相同的,并且可能包含业务逻辑。根据实现的不同,如果将业务逻辑从模型中删除到服务类中,域和 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.

孤独难免 2024-10-02 19:39:20
A DTO = is an object that carries data between processes.

但最有趣的是,它除了存储和检索自己的数据之外没有任何行为!

坚持使用 MVC 方法...

Domain = subject of your entire application.

Model = contains the (programming languages objects : EX: C# objects) to make up the universe of your application.

它们显然可以具有行为和属性(参见与 DTO 的区别)。

通常,一个应用程序(轻型应用程序)可以有一个模型 - 在这种情况下,您的模型正是您的领域。
另一种模型可以是完全不同的对象类型,即处理另一种对象类型。在这种情况下,它们都是您的域的一部分,并被命名为“域模型 - 对象”。

希望这个答案是详尽的并让您明白!

A DTO = is an object that carries data between processes.

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...

Domain = subject of your entire application.

Model = contains the (programming languages objects : EX: C# objects) to make up the universe of your application.

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 !

黑白记忆 2024-10-02 19:39:20

我的理解(简而言之)如下:

(MVC)模型对象:

  • 代表某些使用上下文中的某些事物,例如。 PersonEditModelPersonViewModel 或只是 PersonModel
  • 没有业务逻辑,
  • 可以是某些验证逻辑等的主题,
  • 用于从一个应用程序层向另一应用程序层提供数据例如。 MVC 控制器<-> MVC视图

域对象:

  • 表示一些业务对象(问题域中的现实世界对象),
  • 其业务逻辑
  • 不允许无效的对象状态,具有正确更改对象状态的方法,
  • 用于封装与其相关的业务逻辑
  • 不得用于保存数据(甚至不应该)

DTO(数据传输对象):

  • 与模型对象类似,但应具有扁平结构,
  • 仅简单类型属性/字段(字符串、数字、日期时间、布尔值)
  • 用于跨应用程序边界传输数据,例如。网络服务器和网络浏览器之间

My understanding (in a big short) is as follows:

(MVC) model object:

  • represents some things in a some usage context eg. PersonEditModel, PersonViewModel or just PersonModel
  • has no business logic
  • can be subject of some validation logic etc.
  • used to provide data from one application layer to another eg. MVC controller <-> MVC view

Domain object:

  • represents some business object (real world object in the problem domain)
  • has business logic
  • do not allow invalid object state, has methods to properly change object's state
  • used to encapsulate business logic related to it
  • have not to be used to persist data (or even should not)

DTO (Data Transfer Object):

  • similar to model object but should have flat structure
  • only simple type properties/fields (strings, numbers, datetimes, booleans)
  • used to transfer data cross application boundaries eg. between web server and web browser
ι不睡觉的鱼゛ 2024-10-02 19:39:20

域和 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.

诠释孤独 2024-10-02 19:39:20

MVC 和 DDD 可以一起使用。我们在 DDD 和 MVC 中所说的“模型”实际上是相同的:抽象。我们可以使用伪代码来说明一些示例。

模型视图控制器 (MVC)

模型视图控制器架构将软件分为三个部分:

模型层

MVC 架构中的模型层是逻辑所在的位置。在这一层中,我们有我们的模型和业务逻辑。

class Car {

  String color;
  String year;  

  Cat(color, year) {
    this.color = color;
    this.year = year;
  }

  //getters & setters
}

一个简单的汽车抽象。

class CarService {

  save(car) {
    if(car.getColor() != null && car.getYear() != null) {
      methodToSave(car);
    } else {
      throwsException();
    }
  }

  find(car) {
    return methodToFind(car);
  }

  update(car) {
    assertThatExists(car);
    methodToSave(car);
  }

  delete(car) {
    assertThatExists(car);
    methodToDelete(car);
  }
}

使用服务的汽车的简单 CRUD

视图层

视图层是用户界面驻留。用户可以在此处与系统交互,然后系统将触发所执行的操作的控制器,然后控制器将通知模型层并请求数据。视图层可以驻留在应用程序的客户端或应用程序的服务器端(即:JSF(Java Server Faces)作为服务器端,ReactJS 作为客户端)。不管怎样,即使View层驻留在客户端,客户端也需要请求服务器端发送请求。这可以通过基于 Web 应用程序的 HTTP 请求来完成。

<theCarPage>
  <theCar>
    getTheCarOnLoad();
  </theCar>
</theCarPage>

汽车的伪页面。

控制器层

控制器层基本上接收来自视图的输入,然后转换数据并将其发送到模型层,反之亦然。

class CarController {
  
  @OnLoadingTheCarPage
  getTheCarOnLoad() {
     return theCar();
  }

}

加载汽车的方法。

领域驱动设计(DDD)

领域驱动设计是一个概念:
DDD 的概念奠定了基础,即类、类变量和类方法必须与其核心业务领域相匹配。

领域驱动设计驻留在“M”中

在这种情况下,当应用 MVC 时,领域驱动设计设计驻留在 MVC 架构的模型层中。如前所述,模型层是应用程序的业务逻辑所在的位置。

无论您是否有实体,它们仍然是模型。模型只是现实世界中某些事物的抽象。如果抽象的话,猫可以是模型:

class Cat {
  
  String color;
  String age;  

  Cat(color, age) {
    this.color = color;
    this.age = age;
  }

  //getters & setters
}

简单的猫抽象。 的模型。

它是 Cat. DDD 实体

在领域驱动设计中,我们有实体,它们也被归类为模型。它们之间的区别在于实体是可识别的。如果您有一个可识别且可以持久化的类,那么它就是一个实体。实体仍然是模型。

@AnEntity
@ThisCanBePersisted
class Cat {
  
  @ThisIsAnId
  @ThisValueIncrementsAutomatically   
  @PersistentProperty
  Long id;
  @PersistentProperty
  String color;
  @PersistentProperty
  String age;  

  Cat(color, age) {
    this.color = color;
    this.age = age;
  }

  //getters & setters
}

一个简单的实体。实体就是模型。

数据传输对象 (DTO)

数据传输对象内部没有逻辑,它们的唯一用途是作为将数据从一个端点传输到另一个端点的容器。通常企业实体本质上是不可序列化的,因此我们需要一种方法来仅发送需要发送给客户端的数据。
例如,由于模型可能具有敏感数据或我们不想在获取请求中共享的简单数据,那么考虑到我们的 Cat 模型,我们可以创建一个不共享 Cat ID 的 DTO:

class CatDTO {

  String color;
  String age;  

  //getters & setters
}

A Cat 的数据传输对象。我们只需要它的属性以及获取和设置属性的东西。我们不想共享它的 ID。

因此,例如,如果我们必须使用 REST 从客户端请求所有猫的列表,那么我们将请求使用 CatDTO 而不是 Cat 进行响应的端点实体:

[
  Cat {
    "color": "yellow",
    "age": "1"
  },
  Cat {
    "color": "black",
    "age": "4"
  }
]

这就是我们的客户可以看到的所有数据。

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.

class Car {

  String color;
  String year;  

  Cat(color, year) {
    this.color = color;
    this.year = year;
  }

  //getters & setters
}

A simple Car abstraction.

class CarService {

  save(car) {
    if(car.getColor() != null && car.getYear() != null) {
      methodToSave(car);
    } else {
      throwsException();
    }
  }

  find(car) {
    return methodToFind(car);
  }

  update(car) {
    assertThatExists(car);
    methodToSave(car);
  }

  delete(car) {
    assertThatExists(car);
    methodToDelete(car);
  }
}

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.

<theCarPage>
  <theCar>
    getTheCarOnLoad();
  </theCar>
</theCarPage>

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.

class CarController {
  
  @OnLoadingTheCarPage
  getTheCarOnLoad() {
     return theCar();
  }

}

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:

class Cat {
  
  String color;
  String age;  

  Cat(color, age) {
    this.color = color;
    this.age = age;
  }

  //getters & setters
}

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.

@AnEntity
@ThisCanBePersisted
class Cat {
  
  @ThisIsAnId
  @ThisValueIncrementsAutomatically   
  @PersistentProperty
  Long id;
  @PersistentProperty
  String color;
  @PersistentProperty
  String age;  

  Cat(color, age) {
    this.color = color;
    this.age = age;
  }

  //getters & setters
}

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:

class CatDTO {

  String color;
  String age;  

  //getters & setters
}

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:

[
  Cat {
    "color": "yellow",
    "age": "1"
  },
  Cat {
    "color": "black",
    "age": "4"
  }
]

And that would be all the data that our client could see.

各自安好 2024-10-02 19:39:20

大多数对象的定义根据对象的使用位置而有所不同:

模型:是在客户端服务器中使用对象通用定义>.

  1. 模型视图:是大多数时候在客户端中使用的对象
  2. Domain Object:是服务器中使用的对象将数据传输到数据库
  3. 数据传输对象(DTO):是一种将数据从一个对象传输到另一个对象的对象,特别是在API调用(例如:在用于获取数据的 api GET Method 调用中,您不得向客户端提供数据库模型,为此您使用 dto)。

注意:这些定义在大多数情况下都是正确的,但在某些情况下并不实用。

Any definition for most of the objects is various based on place of using of objects:

Model: is a general definition for using object in client or server.

  1. Model View : is a object using in client most of the time.
  2. Domain Object : is a object using in server and transfering data to the database.
  3. Data Transfer Object(DTO) : is a object that transfer data from one object to another object, specially in getting data in API Call(for example: in api GET Method call for getting data you must not to give database models to client, for this purpose you use dto).

Notice: the definitions are true most of the time but in some situations arent't practical.

把昨日还给我 2024-10-02 19:39:20

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.

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