このドキュメントでは、Effect-TSを用いた関数型DDD、CQRS、イベントソーシングによる履修管理システムの実装方法を説明します。科目の追加・削除は一括操作として実装されています。
- CQRS (Command Query Responsibility Segregation): コマンド(書き込み)とクエリ(読み込み)を分離
- イベントソーシング: 状態変更をイベントとして記録(Update禁止、Insertのみ)
| let result = 0; | |
| [1,2,3,4,5,6].forEach((n) => { | |
| if (n % 2 === 0) { | |
| result += n * n; | |
| } | |
| }); | |
| console.log(result); |
| const result = [1,2,3,4,5,6] | |
| .filter(n => n % 2 === 0) | |
| .map(n => n * n) | |
| .reduce((a, b) => a + b, 0); | |
| console.log(result); // (2*2) + (4*4) + (6*6) |
| // https://github.com/j5ik2o/ticket-price-modeling | |
| // 解き方方針: 型で問題を解くよりも、料金表のように日本人の非エンジニアの人も読めて突っ込めるコード記述を目指す. | |
| // 解き方方針: 配列でmap&sortの世界に引き込んで安い料金を選ぶようにする. | |
| // 解いたのは "シネマシティズン" の場合だけ。 | |
| // TODO 引数を型にして呼び出しに制約を設けるかはあとで考える。 | |
| const movePayment = (ticketType, timeTypes) => { | |
| var payments = | |
| {"シネマシティズン": | |
| {"平日〜20:00": 1000, |
| interface Expression { | |
| times(muptiplier: number): Expression; | |
| plus(addend: Expression): Expression; | |
| reduce(bank: Bank, to: string): Money | |
| } | |
| class Money implements Expression { | |
| private _amount: number | |
| private _currency: string |
| function createReportData(readList, recommendList) { | |
| const reportData = {}; | |
| reportData.userName = readList.name; | |
| reportData.readBooks = readList.books | |
| .map(book => { | |
| return {...book, point: point(book)}; | |
| }); | |
| reportData.total = total(reportData); | |
| return reportData; |
| const createReportData = require('./createReportData'); | |
| function generateReadReport(readList, recommendList) { | |
| const reportData = createReportData(readList, recommendList); | |
| return renderPlainText(reportData); | |
| } | |
| function renderPlainText(reportData) { | |
| let report = `name: ${reportData.userName}\n`; | |
| report += '-----\n'; |