首页 > 解决方案 > 软件设计:数据模型的使用和服务之间的依赖关系

问题描述

我想听听一些关于软件设计的意见:

  1. 数据模型

我目前正在开发基于以下的软件:

Controller
    Service
        Repository

而且我不确定在每一层中使用不同的数据模型是否是一种好习惯。

Controller -> Receives a DTO and converts it into a "service object"
    Service -> Receives a "service object" and converts it into a database entity
        Repository -> persists a database entity

看起来有很多重复的代码,因为通常每一层的信息交换没有太大区别。

  1. 服务之间的依赖

假设您有两个服务 可用性:检查可用日期 预订:进行预订

现在,如果在调用可用性服务时,该日期可用,则必须预订该日期。应该如何管理这种依赖关系?

- Option 1: calling BookingService from inside AvailabilityService
    AvailabilityService {

        Calling BookingService

    }

- Option 2: calling BookingService after the response of AvailabilityService
    AvailabilityController {
        Calling AvailabilityService
        Calling BookingService (base on the response from AvailabilityService)
    }

标签: architecture

解决方案


问题 1:模型取决于谁在“处理”数据。具有 O/R 映射器的持久层具有特定的对象建模方式(或者更准确地说:类)。另一方面:浏览器中的视图(不在数据库中)通常混合来自不同对象的数据,因此您为该视图创建了一个自定义的 DTO,其中包含该视图需要显示的内容。这还包括不公开视图不需要的数据并避免多个请求(首先是这个对象,然后是那个......)。如果某人需要另一个数据结构,则必须决定是否需要进一步转换。我认为这不是一个常见的用例,通常您必须将数据结构从数据库映射到视图所需的数据。

问题 2:如果您的 AvailabilityService 仅检查可用性,则不应预订。否则名称将是错误的。在选项 2 中,控制器的名称是错误的,这显然不是可用性控制器,而是类似于 OrderController,请注意选择好名称(清洁代码)。


推荐阅读