c# - 将 DTO 传递给服务层

标签 c# java design-patterns multi-tier three-tier

将 DTO 对象传递给服务层不是不好的做法吗?

现在我的服务层方法是这样的:

public save(MyEntity entity);

将值从 DTO 映射到业务实体 (MyEntity) 在表示层完成

但我想将方法​​签名更改为:

public save(MyEntityDTO dto, String author);

之后,从 DTO 到业务实体的映射将发生在服务层。

编辑:我想要它是因为在从 DTO 映射到业务对象时我需要打开 hibernate session ,因此实体上的所有更改都将自动刷新。

最佳答案

Is it not bad practice to pass DTO object to service layer?

不仅可以将 DTO 对象传递给服务层,而且应该将 DTO 对象而不是业务实体传递给服务层。

Your service should receive DTOs, map them to business entities and send them to the repository. It should also retrieve business entities from the repository, map them to DTOs and return the DTOs as reponses. So your business entities never get out from the business layer, only the DTOs do.

查看类似问题的完整答案 here .

关于c# - 将 DTO 传递给服务层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20092440/

相关文章:

C# 位置0处没有行

java - 使用 Selenium 的页面对象设计模式是否创建页面类并通过它们的对象调用它们的构造函数?

c# - 员工休假计划的正确设计模式?

java - 对 Web 服务客户端隐藏数据对象结构和字段

java - 在 Apache Spark 上为每个 worker 创建一个单例

c# - Linq大量更新时极慢

C# 从视频文件的一部分中提取帧

c# - 如何防止 XtraGrid TextEdit 在将光标移动到内容之外时失去焦点

java - Java 控制台应用程序中使用 Windows "cls"命令清屏

java - Java计算器程序中do while循环的执行