【问题标题】:How to implement repository to clean architecture?如何实现存储库来清理架构?
【发布时间】:2022-10-13 02:35:40
【问题描述】:

我正在为我的医疗应用程序 API 遵循 Bob 叔叔的干净架构,但我在哪里实现某些事情时遇到了逻辑问题。

在我的应用程序层中,我有我的CreateTreatmentPlanUseCase,我在其中注入了两个存储库:AbstractPatientRepo 和 AbstractTreatmentPlanRepo。

export class CreateTreatmentPlanUseCase implements UseCase<CreateTreatmentPlanRequestDto, Promise<Response>> {
    protected patientRepository: AbstractPatientRepository
    protected treatmentPlanRepository: AbstractTreatmentPlanRepository

    constructor(
        patientRepository: AbstractPatientRepository,
        treatmentPlanRepository: AbstractTreatmentPlanRepository
    ) {
        this.patientRepository = patientRepository
        this.treatmentPlanRepository = treatmentPlanRepository
    }
}

在我的接口适配器层中,我在web 目录中有一个控制器

export default class CreateTreatmentPlanController {
    protected useCase: CreateTreatmentPlanUseCase

    constructor(useCase: CreateTreatmentPlanUseCase) {
        this.useCase = useCase
    }

    public async execute(req: express.Request, res: express.Response) {
        const dto = <CreateTreatmentPlanRequestDto>req.body

        const treatmentPlanOrError = await this.useCase.execute(dto)

        if (treatmentPlanOrError.isFailure()) {

        }
    }
}

所以我需要将CreateTreatmentPlanUseCase 注入我的CreateTreatmentPlanController

  1. 我需要在哪里构造CreateTreatmentPlanUseCaseCreateTreatmentPlanController?这仍然发生在我的interface adapters 层中还是发生在frameworks/ infrastrcture 层中?

【问题讨论】:

    标签: clean-architecture


    【解决方案1】:

    在 Clean Architecture 中,应用程序的“组合”发生在“主要组件”中。这是我们连接所有这些细节并进行依赖注入的地方。

    您可以将此组件视为基础架构层的一部分或作为完整架构的最外层。 有些人甚至建议为此设立专门的项目:https://medium.com/@gsferreira/the-missing-project-that-fixes-everything-in-net-5ca5ba35c6ae

    在 CLI 程序中,这可能是“主要”入口点。在 Web 应用程序中,它可能是您配置 Web 框架的地方,例如 Asp.Net 中的“Startup.cs”。

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 2021-01-22
      • 2012-05-13
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 2020-07-07
      • 2011-03-23
      • 2019-05-15
      相关资源
      最近更新 更多