I am implementing clean architecture in my new back-end project and getting stuck with a particular use case.

I have a CreatePhysicianUseCase what is responsible for creating a new physician user in my system. I inject my repository into it which has the following methods:

export interface CreatePhysicianRepository {
    userExistsById(userId: string): Promise<boolean>

    physicianExistsByUserId(userId: string): Promise<boolean>

    createUser(): Promise<any>

    countryExistsById(): Promise<boolean>

    departmentExistsById(): Promise<boolean>

    hospitalExistsById(): Promise<boolean>

    professionExistsById(): Promise<boolean>

    createPhysician(physician: Physician): Promise<Physician>

This is the input I am expecting from clients:

export interface CreatePhysicianRequestDto {
    firstName: string
    lastName: string
    professionId: string
    departmentId: string
    hospitalId: string
    countryId: string

Somewhere I need to verify that the provided departmentId, professionId, hospitalId and countryId exists in my database. But I feel like this is not the responsibility of the CreatePhysicianUseCase. What would be the proper solution?

Option 1

In my CreatePhysicianController do I inject multiple useCases which will verify that the required, for example, profession exists and remove the hospitalExistsById() from the CreatePhysicianRepository interface?

Option 2

Do I keep the methods in the CreatePhysicianRepository and inject respective DataSources to the repository in my main module? I know I need to check some of the methods (like hospitalExistsById) in other useCases as well so if I add these into multiple repositories wouldn't that be a violation of DRY?

Option 3

Do I create a common repository for example for Country which will have countryExistsById method in and I will inject this into the CreatePhysicianUseCase?

Maybe there is a fourth option I haven't come up with?

  • 1
    Tell, don't ask. Tell the repository to save the new input and let the repository (black box) deal with the ER integrity.
    – Laiv
    Commented Nov 30, 2022 at 12:30


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.