概要
設計の脳内整理資料
ディレクトリ構成
プレゼンテーション層
ルーター
routes\task.route.ts
import express from 'express';
import TaskApplication from '../applications/task.application';
import TaskRepository from '../infrastructure/task.repository';
const taskRepository = new TaskRepository();
const taskApplication = new TaskApplication(taskRepository);
const router = express.Router();
router.get('/', async (req: express.Request, res: express.Response, next: express.NextFunction) => {
const data = await taskApplication.getData();
res.json({
data
});
});
export default router
アプリケーション層
アプリケーション
applications\task.application.ts
import { ITaskRepository } from "../domains/task.repository.interface";
export default class TaskApplication {
private taskRepository: ITaskRepository
constructor(taskRepository: ITaskRepository) {
this.taskRepository = taskRepository
}
async getData(): Promise<any> {
const data = await this.taskRepository.find();
const resdata = data.map((d) => ({
id: d.id,
title: d.title,
description: d.description
}))
return resdata
}
}
ドメイン層
ドメインモデル
domains\task.model.ts
type TaskElement = {
id: number | null
title: string
description: string
}
export class Task {
readonly #element: TaskElement
get id() { return this.#element.id }
get title() { return this.#element.title }
get description() { return this.#element.description }
constructor(element: Partial<TaskElement>) {
this.#element = {
id: element.id ?? null,
title: element.title ?? '',
description: element.description ?? ''
}
}
}
ドメインファクトリ
// 検討中
ドメインサービス
// 検討中
リポジトリインターフェイス
domains\task.repository.interface.ts
import { Task } from "./task.model";
export abstract class ITaskRepository {
abstract find(): Promise<Task[]>
}
インフラストラクチャ層
リポジトリ実装
infrastructure\task.repository.ts
import { Task } from "../domains/task.model";
import { ITaskRepository } from "../domains/task.repository.interface";
import { TaskTable } from "./db/task.table";
export default class TaskRepository extends ITaskRepository {
async find() {
const taskTable = new TaskTable();
const dataList = await taskTable.getTask();
const sampleTask = dataList.map((data) => {
return new Task(data);
})
return sampleTask;
}
}
DBクエリ発行
infrastructure\db\task.table.ts
export class TaskTable {
private table: string = 'task'
constructor() { }
async getTask(id?: number) {
// 本来はDB接続、クエリ発行などをここで実装する
const result = [{
id: 1,
title: 'テストタスク1',
description: 'これはテストです。'
}]
return result;
}
}
↧