10

在此处输入图像描述

域层通过数据传输对象 (DTO) 与其他层通信。我对 DTO 感到困惑。

DTO 1 位于域和表示层之间。

DTO 2 位于域和数据层之间。

我应该在层之间创建两个不同的 DTO 对象还是只创建一个 DTO。哪个是专业的方式?

4

4 回答 4

6

让我们遍历所有层:

  • 数据访问层 (DAL)。它用于从数据库(DB)中获取数据。

通常它知道Domain Entities域层。

DAL 可以返回Domain EntitiesDTOs (DB oriented data structures)。如果需要,可以使用这些 DTO 或域实体来构建表示层 ( view models) 的 DTO。

域实体通常很重,需要数据映射器或任何 ORM。我更喜欢使用Domain Entities、映射它们并避免使用其他 DTO。否则 DTO 也应该被映射。

  • 领域层(领域模型)。它用于表示业务实体及其行为、业务规则、纯业务逻辑。

域层应该对实体存储在某处(例如在数据库中)的方式一无所知。它可以有自己的 DTO,这可以是重构Introduce Parameter Object的结果。

  • 表示层 (UI)。它用于向用户呈现 UI。

它应该知道即将Data Access Layer从数据库加载数据并Domain Layer访问其业务逻辑。

它可以有自己的 DTO - 视图模型,它们是域实体的用户界面友好表示或 DB 友好 DTO。表示层有责任了解view models.

如果您将只有一个演示文稿,您的应用程序基础架构也可以作为演示层的一部分实现,但通常它是一个单独的应用程序层。

在此处输入图像描述

于 2013-11-04T21:40:49.877 回答
2

It really depends on your your specific needs.

In a general sense, you should create 2 sets of DTO's. This allows for better de-coupling of different layers and makes the architecture of your system more flexible. The specific reasons or cases where it is needed are for example:

  • Sharing DTO's may not be possible, e.g. because there are differences in technologies used, e.g. a web service and data layer are written in C# and the presentation layer is written in Java.
  • DTO's are not necessarily the same, i.e. your DTO for the interaction with the database layer may be modelled on the database structure but you may expose it differently to the presentation layer.

Having said that, if you can live with the limitations of having one set of DTO's, you can share them if it suits your needs as it produces less code to write and maintain.

于 2013-11-02T21:03:55.337 回答
1

您的图像显示了两个名为 DTO1 和 DTO2 的 DTO 对象。

DTO1 在表示层和域层之间共享数据。您可以将它们称为ViewModel类。

DTO2 在域和数据层之间共享数据。您可以将它们称为数据传输对象 ( DTO )。

所以你可以使用两个不同的传输对象。

于 2013-11-03T21:09:32.453 回答
0

如果我们假设您使用两个单独的 DTO(DTO1 和 DTO2),
答案很简单:

在这种情况下您必须创建两个单独的 DTO:DTO1 和 DTO2。
即使它们是相同的,它们也应该作为单独的类来实现。

这是因为 DTO1 是在域层中创建的,而 DTO2 是在数据层中创建的(根据您的图片)。

请注意,在某些解决方案中,不使用两个 DTO - 有时,只有一个 DTO。

于 2014-01-07T13:31:38.090 回答