我想听听一些关于软件设计的意见:
- 数据模型
我目前正在开发基于以下内容的软件:
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
看起来有很多重复的代码,因为通常每一层的信息交换差别不大。
- 服务之间的依赖关系
假设您有两项服务 可用性:检查可用日期 预订:进行预订
现在,如果在调用可用性服务时,该日期可用,则必须预订该日期。应如何管理这种依赖性?
- 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)
}
最佳答案
问题1: 该模型取决于谁在“处理”数据。带有 O/R 映射器的持久层有一定的对象建模方式(或者更准确地说:类)。另一方面:浏览器中的 View (而不是数据库中的 View )通常会混合来自不同对象的数据,因此您可以为该 View 创建一个自定义的 DTO,其中包含该 View 需要显示的内容。这还包括不公开 View 不需要的数据并避免多个请求(首先是这个对象,然后是那个......)。是否需要进一步转换必须决定是否有人需要其他数据结构。我认为这不是一个常见的用例,通常您必须将数据结构从数据库映射到 View 所需的数据。
问题2: 如果您的 AvailabilityService 仅检查可用性,则不应进行预订。否则这个名字就是假的。在选项 2 中, Controller 的名称是错误的,这显然不是 AvailabilityController,而是类似 OrderController 的东西,请注意选择好名称(Clean Code)。
关于architecture - 软件设计: use of data models and dependency between services,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54272715/