『Domain Modeling Made Functional』
https://gyazo.com/15c85c992c58636fa61efb694ef31c2c
Scott Wlaschin著
The Progmatic Bookshelf
/mrsekut-book-97816805025
F#でDDDする本
要は関数型言語でDDDする本
かなり型駆動開発の趣が強い
英語が平易で読みやすいmrsekut.iconmrsekut.icon
この本を読むためにF#の事前知識はどれほど必要か
4章で型の説明があるので「関数型言語」の知識はなくても耐えそうだが、
実際、関数型初見マンがこれぐらいの薄い説明で納得できるのかは微妙だと思う
関数型の経験があれば4章は不要レベル
今までF#のままメモってたが意味ない無いのでできるだけhsで書こうmrsekut.icon
ただ、F#でのこれって、HSでのどれだっけ?になる場合はF#書く
実装
https://pragprog.com/titles/swdddf/#resources
https://github.com/swlaschin/DomainModelingMadeFunctional
F#
Haskell
https://github.com/bontaq/domain-modeling
https://github.com/nakaakist/haskell-ddd-order-taking
https://github.com/chriskrycho/dmmf
F#
Elm
Rust
ReasonML
いっぱいあるのは良いけど本のものを完全に実装してない感じがある
https://github.com/andorp/order-taking
Idris
この本の中で作ろうとしているものの要件のmemo
「Domain Modeling Made Functional」内の要件や用語
Part I ドメインを理解する
1章
/mrsekut-book-97816805025/018 (CHAPTER1 Introducing Domain-Driven Design)
モデルを中心に据えてDomain Expertと議論する
Domain Eventに着目することで、ドメイン理解を深める
これ、のちの型でワークフローを書くことの伏線になっているのかmrsekut.icon
Subdomainに分解する
ユビキタス言語
2章
/mrsekut-book-97816805025/040(CHAPTER 2 Understanding the Domain)
Domain Expertにインタビューする
インタビューの過程が説明されている
Documenting the Domain Modeling in Simple DSL
Domain Expertから学んだことをDSLで表現する
個々の値の上限・下限
ワークフローを型で定義
3章
/mrsekut-book-97816805025/058 (CНАРТER 3 A Functional Architecture)
関数指向のドメインモデルのためのアーキテクチャについて
C4
Bounded Context
/mrsekut-book-97816805025/065 (Workflows Within a Bounded Context)~
Part II ドメインモデリングをする
この本特有の言葉
choice type
直和型、Union型のこと
Simple Types
Haskellのnewtypeと同じ
code:fsharp
type ProductCode = ProductCode of string // ProductCodeはユビキタス言語
// こう書いているのに近い. singleのunion型
type ProductCode =
| ProductCode of string
// 利用
let productCode = ProductCode 42
ユビキタス言語を活用するために、pritimive型を逐次simple typeにする
4章 Understanding Types
/mrsekut-book-97816805025/074 (CHAPTER 4 Understanding Types)
F#の超基本的な型の解説
型シグネチャとか、代数的データ型とか
関数型言語を少しでもしてたら既に知っているようなこと
ドメインでの処理の流れをそのまま型で表現しちゃえばいい
code:fsharp
type PayInvoice =
UnpaidInvoid -> Payment -> Result<PaidInvoice, PaymentError>
未払い請求書があり、決済が行われたら、支払い済み請求書になる
ディレクトリ構成の話
型用のファイルと関数用のファイルを分ける
ファイル内で書く順番によって使用できなかったりするので注意
5章 Domain Modeling with Types
/mrsekut-book-97816805025/092 (CHAPTER 5 Domain Modeling with Types)
ドメインモデルをF#の型で表現していく
Type Level Domain Modeling
この辺は『エリック・エヴァンスのドメイン駆動設計』.iconと同じことを言っているmrsekut.icon
A Question of Identity: ValueObject p.88
A Question of Identity: Entity p.89
Aggregate
6章 Integrity and Consistency in the Domain
/mrsekut-book-97816805025/118 (CHAPTER 6 Integrity and Consistency in the Domain)
この辺はもう、型のみで完結できる範囲を少し超えていたりするmrsekut.icon
trustedなDomainには、IntegrityとConsistencyの2つの側面がある
Integrity
Consistency
この2つに分ける必然性が微妙にわからないmrsekut.icon
Starbucks Does Not Use Two-Phase Commit
日本のスターバックスのレジでの非同期的な処理を例にして説明している
同じBounded Context内でのConsistencyの例示が衝撃を受けて良かったmrsekut.icon
7章 Modeling Workflows as Pipelines
/mrsekut-book-97816805025/134 (CHAPTER 7 Modeling Workflows as Pipelines)
/mrsekut-book-97816805025/第7章 ワークフローをパイプラインとしてモデル化する
transformation-oriented programming
Part III Implementing the Model
/mrsekut-book-97816805025/160 (Part III Implementing the Model)
8 Understanding Functions
/mrsekut-book-97816805025/162 (CHAPTER 8 Understanding Functions)
FPの基本的なことの解説
高階関数
カリー化
F#もhsと同様に全ての関数はカリー化される
部分適用
常に全域関数を定義
p.154~の例って、Intを引数に取っているのに0~6しかmatchさせないことに触れてないけどそもそも嘘じゃない?例が良くないmrsekut.icon
Compositionは関数結合のこと
F#ではよく|>を使う
ほぼ既知だったので軽く読み飛ばしたmrsekut.icon
9 Implementation: Composing a Pipeline
/mrsekut-book-97816805025/176 (CHAPTER 9 Implementation: Composing a Pipeline)
/mrsekut-book-97816805025/第9章 実装: パイプラインの構成
最終的にはこういう感じで処理を書きたい
code:fs
let placeOrder unvalidatedOrder =
unvalidated0rder
|> validateOrder
|> priceOrder
|> acknowledgeOrder
|> createEvents
7章までで、各stepの型を定義したので、ここから内部を実装していく
最終的に上のように連結したいが以下のような困難が出てくるので工夫が必要
副作用の扱い
前のoutputと次のinputが異なる
別の場所から引数を取ってこないといけないケースがある
etc.
9章で副作用を無視したDIについて、10章でinput/outputの乖離の問題を扱う?
/mrsekut-book-97816805025/177 (Working with SimpleTypes)~
/mrsekut-book-97816805025/178 (Using Function Types to Guide the Implementation)~
/mrsekut-book-97816805025/187 (Implementing the Rest of the Steps)~
この辺はsmart constructorをちゃんと作ろうな、という話を具体例を用いてしている
まだ各stepの定義をしているだけ
次のinputのために、outputを変換する手法は紹介されている
次のinputの引数を別の場所から取ってくる方法まだなので、そのままではcomposeできない
/mrsekut-book-97816805025/193 (Composing the Pipeline Steps Together)
composeする方法
Injecting Dependencies
関数に対してDI
基本は、関数の引数に関数を渡すDIをする
場所によっては、カリー化でDIする
前の節を読んでないので、個々の関数がどういうものかを知らずに読んだ
要旨はわかった気がするが、前から順に読む時に再読しても良さそうmrsekut.icon
Testing Dependencies
前章の内容を前提としたtest
The Assembled Pipeline 187(3)
Wrapping Up 190(1)
10 Implementation: Working with Errors 191(30)
/mrsekut-book-97816805025/206 (CHAPTER 10 Implementation: Working with Errors)
Result型を使ってerrorを扱う
起きうるErrorの種類が型で明示される
Errorを3つに分類する
鉄道指向プログラミング
Using the Result Type to Make Errors 191(1)
Explicit
Working with Domain Errors 192(4)
Chaining Result-Generating Functions 196(7)
Using bind and map in Our Pipeline 203(2)
Adapting Other Kinds of Functions to the 205(4)
Two-Track Model
Making Life Easier with Computation 209(8)
Expressions
Monads and More 217(1)
Adding the Async Effect 218(2)
Wrapping Up 220(1)
11 Serialization
/mrsekut-book-97816805025/236 (Chapter 11 Serialization)
/mrsekut-book-97816805025/第11章 シリアライゼーション
Domain Type, DTO, JSON等、間の変換をする
前と同様pipelineで
型の種類ごとのDTOの表現方法を解説している
12 Persistence
/mrsekut-book-97816805025/254 (Chapter 12 Persistence)
Pushing Persistence to the Edges 239(5)
Command-Query Separation 244(4)
Bounded Contexts Must Own Their Data 248(2)
Storage
Working with Document Databases 250(1)
Working with Relational Databases 251(11)
Transactions 262(1)
Wrapping Up 263(2)
13 Evolving a Design and Keeping It Clean 265(20)
/mrsekut-book-97816805025/280 (Chapter13 Evolving a Design and Keeping It Clean)
Change 1 Adding Shipping Charges 266(4)
Change 2 Adding Support for VIP Customers 270(3)
Change 3 Adding Support for Promotion 273(7)
Codes
Change 4 Adding a Business Hours 280(1)
Constraint
Dealing with Additional Requirements 281(1)
Changes
Wrapping Up 282(1)
Wrapping Up the Book 283(2)
Index 285
https://www.youtube.com/watch?v=srQt1NAHYC0
著者の公演
書評
https://qiita.com/yasuabe2613/items/5ab33e103e4105630e4c
https://blog.shin1x1.com/entry/domain-modeling-made-functional
https://www.slideshare.net/secret/Jj8eHp7oOsylDe
#スクボ読書化した本