architecture - 软件设计:数据模型的使用和服务之间的依赖关系
问题描述
我想听听一些关于软件设计的意见:
- 数据模型
我目前正在开发基于以下的软件:
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 映射器的持久层具有特定的对象建模方式(或者更准确地说:类)。另一方面:浏览器中的视图(不在数据库中)通常混合来自不同对象的数据,因此您为该视图创建了一个自定义的 DTO,其中包含该视图需要显示的内容。这还包括不公开视图不需要的数据并避免多个请求(首先是这个对象,然后是那个......)。如果某人需要另一个数据结构,则必须决定是否需要进一步转换。我认为这不是一个常见的用例,通常您必须将数据结构从数据库映射到视图所需的数据。
问题 2:如果您的 AvailabilityService 仅检查可用性,则不应预订。否则名称将是错误的。在选项 2 中,控制器的名称是错误的,这显然不是可用性控制器,而是类似于 OrderController,请注意选择好名称(清洁代码)。
推荐阅读
- php - 如何将端口80重定向到另一个端口
- matlab - 连接矩阵
- docker - 使用 koxudaxi/local-data-api 在容器中本地调用 Aurora Serverless 集群时出错
- visual-studio-code - 当文件明显存在时,PIO 编译器说没有这样的文件或目录
- sql - 从表中的重复项中选择最小日期记录
- flutter - 如何在颤动中删除小部件之间的空间
- javascript - 在 React 中将数据传递给父级时,根据子级操作重新渲染子组件
- .net-core - 如何使用“Dotnet dotcover test --dcFilters”命令将某些命名空间排除在代码覆盖范围之外
- html - CSS裁剪图像高度而不改变其宽度
- statistics - 监控 Google Docs 上的编辑统计信息