如若库层返回数据传输对象(DTO)?

编程入门 行业动态 更新时间:2024-10-27 00:24:03
本文介绍了如若库层返回数据传输对象(DTO)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个库层,负责我的数据访问,这是由一个服务层调用。服务层返回的DTO这是序列化并通过网络发送。通常情况下,服务做的多一点访问存储库,并返回无论存储库的回报。

I have a repository layer that is responsible for my data-access, which is called by a service layer. The service layer returns DTOs which are serialized and sent over the wire. More often than not, services do little more than access a repository and return whatever the repository returns.

但对于工作,库必须返回DTO的一个实例。否则,您必须先映射数据层对象库返回到DTO在服务层和返回。这似乎只是一种浪费。

But for that to work, the repository has to return an instance of that DTO. Otherwise, you would first have to map the data layer object that the repository returns to a DTO in the service layer and return that. That just seems wasteful.

在最重要的是,如果DTO的创作在服务层发生,而这可能是在一个库中调用之前完成,因此,一个数据库查询,现在有在服务层的多个库调用发生谱写最后的DTO。当然,除非我创建一个可以包含这样一个沉稳对象中的数据和服务层之间的传输对象。其中然后的必须映射到DTO。这似乎只是浪费纯度的缘故。但它也觉得不妥有,只是存在通过线路发送存储库层返回的对象。

On top of that, if creation of the DTOs happens in the service layer, something that might have been done before in one repository call and thus one database query, now has to happen with multiple repository calls in the service layer to 'compose' the final DTO. Unless of course I create a transport object for between the data and service layer that can contain such a composed object. Which then has to be mapped to a DTO. It just seems wasteful for the sake of purity. But it also feels wrong to have the repository layer return objects that just exist to be sent over the wire.

推荐答案

简短的回答:没有。

龙答:库的作用是接通持续的数据回实体(模型),反之亦然。

Long answer: repository is responsible for turning persisted data back to entities (models) and vice versa.

模式是一种商业模式重新presenting一个商业实体。 DTO在另一方面 - 虽然貌似模式 - 关注各种环境之间在本质上对象的转移是一个短暂的对象。通常的映射器负责翻模成DTO。

Model is a business Model representing a business entity. DTO on the other hand - while looks like Model - is concerned with transfer of the object between various environment and in essence is a transient object. Usually mappers are responsible for turning model into DTO.

更多推荐

如若库层返回数据传输对象(DTO)?

本文发布于:2023-07-27 11:44:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据传输   对象   DTO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!