什么是数据传输对象 (DTO)? [英] What is a Data Transfer Object (DTO)?

查看:53
本文介绍了什么是数据传输对象 (DTO)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是数据传输对象?

MVC 中的模型类是 DTO,如果不是,有什么区别,我们是否需要两者?

In MVC are the model classes DTO, and if not what are the differences and do we need both?

推荐答案

数据传输对象是一种对象,用于封装数据,并将数据从应用程序的一个子系统发送到另一个.

A Data Transfer Object is an object that is used to encapsulate data, and send it from one subsystem of an application to another.

DTO 最常被 N 层应用程序中的服务层用于在其自身和 UI 层之间传输数据.这里的主要好处是它减少了分布式应用程序中需要通过线路发送的数据量.他们还在 MVC 模式中制作了很棒的模型.

DTOs are most commonly used by the Services layer in an N-Tier application to transfer data between itself and the UI layer. The main benefit here is that it reduces the amount of data that needs to be sent across the wire in distributed applications. They also make great models in the MVC pattern.

DTO 的另一个用途是封装方法调用的参数.如果一个方法需要四个或五个以上的参数,这会很有用.

Another use for DTOs can be to encapsulate parameters for method calls. This can be useful if a method takes more than four or five parameters.

使用 DTO 模式时,您还将使用 DTO 汇编器.汇编器用于从域对象创建 DTO,反之亦然.

When using the DTO pattern, you would also make use of DTO assemblers. The assemblers are used to create DTOs from Domain Objects, and vice versa.

从域对象到 DTO 再转换回来可能是一个代价高昂的过程.如果您不创建分布式应用程序,您可能不会从该模式中看到任何好处,如 Martin福勒在此解释.

The conversion from Domain Object to DTO and back again can be a costly process. If you're not creating a distributed application, you probably won't see any great benefits from the pattern, as Martin Fowler explains here.

这篇关于什么是数据传输对象 (DTO)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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