コードの構成は、S00 で見たヘキサゴナルアーキテクチャです。port の型を内側(S06 は useCase、参照実装は domain)に配置するため、domain と useCase は adaptor を import しません。port は S06 で見た、ユースケースが外部に要求する処理の型です。アダプタ(コードでは adaptor)は、外部からの依頼をユースケースへ渡す処理と、port の型を満たす実装です。
実線 = 呼び出し 破線 = import
-
Final のコードは、内側(domain と useCase)と外側(adaptor)に分かれます。adaptor/primary は外部から依頼を受け取る側、adaptor/secondary はユースケースが外部に処理を要求する先です。
examples/final/src/domain/appointment/appointmentStores.ts:16-21type AppointmentStore<TEvent> = Readonly<{ store: (...events: readonly TEvent[]) => ResultAsync<void, AppointmentStoreError>; }>; export type AppointmentBookedStore = AppointmentStore<AppointmentBooked>; export type AppointmentCheckedInStore = AppointmentStore<AppointmentCheckedIn>; export type ExaminationStartedStore = AppointmentStore<ExaminationStarted>; -
port は参照実装では domain に配置した型です。S06 では useCase に配置しました。どちらも内側であるため、内側のコードは外側の実装に依存しません。ExaminationStartedStore は、ExaminationStarted を受け取って ResultAsync を返すという仕様だけを定義します。
examples/final/src/domain/appointment/appointmentStores.ts:16-21type AppointmentStore<TEvent> = Readonly<{ store: (...events: readonly TEvent[]) => ResultAsync<void, AppointmentStoreError>; }>; export type AppointmentBookedStore = AppointmentStore<AppointmentBooked>; export type AppointmentCheckedInStore = AppointmentStore<AppointmentCheckedIn>; export type ExaminationStartedStore = AppointmentStore<ExaminationStarted>; -
ユースケースは、依存を port の型で受け取ります。どの実装が渡されるかには依存しません。
examples/final/src/useCase/startExaminationUseCase.ts:45-51export type Dependencies = Readonly<{ userResolver: UserByIdResolver; appointmentResolver: AppointmentByIdResolver; examinationStartedStore: ExaminationStartedStore; clock: Clock; eventIdGenerator: EventIdGenerator; }>; -
adaptor/secondary の createAppointmentEventStore は、port の型を満たす実装を返します。データベース(SQLite)を扱うコードは、この層にだけあります。
examples/final/src/adaptor/secondary/sqlite/store/appointmentEventStore.ts:144-148export const createAppointmentEventStore = (db: SqliteDatabase) => ({ store: (...events: readonly AppointmentProjectionEvent[]) => ResultAsync.fromPromise<void, AppointmentStoreError>( Promise.resolve().then(() => db.transaction((tx) => { -
app.ts が実装を生成し、ユースケースへ渡します。S06 で確認した、port へ実装を渡す処理です。
examples/final/src/app.ts:408-414startExamination: StartExaminationUseCase.create({ userResolver: clinicUserByIdResolver, appointmentResolver: clinicAppointmentByIdResolver, examinationStartedStore: appointmentEventStore, clock, eventIdGenerator, }), -
実行時は、route がユースケースを呼び出します。ユースケースは domain の関数で判定し、保存は port の型を通して SQLite の store に依頼します。実行時の呼び出しは、外側の route から内側のユースケースと domain を経由して、再び外側の store に至ります。
examples/final/src/adaptor/primary/web/routes/appointmentRoutes.ts:496-497return dependencies.startExamination .run({ actorUserId: actor.value.user.userId, appointmentId: appointmentId.value, veterinarianId }) -
import の矢印はすべて内側を向きます。app.ts が domain、useCase、adaptor を import して組み立てます。domain と useCase は adaptor を import しないため、テストでは代替の実装を渡せます。
examples/final/src/app.ts:408-414startExamination: StartExaminationUseCase.create({ userResolver: clinicUserByIdResolver, appointmentResolver: clinicAppointmentByIdResolver, examinationStartedStore: appointmentEventStore, clock, eventIdGenerator, }),