如若库层返回数据传输对象(DTO)? [英] Should the repository layer return data-transfer-objects (DTO)?

查看:243
本文介绍了如若库层返回数据传输对象(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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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