将 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/