-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
629 lines (445 loc) · 14.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<!DOCTYPE html>
<html>
<head>
<title>Functional Domain Modelling</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# Functional Domain Modelling
---
# Agenda
* Introduction
* Theory
* Techniques
* Example
* Coding Exercise
* Demos and Discussion
---
# Part One - Introduction
---
class: center, middle
# Bytecraft Oy
[`[email protected]`](mailto:[email protected])
[`bytecraft.fi`](https://bytecraft.fi)
---
class: center, middle
Juha Heljoranta
Partner and Software Craftsman at Bytecraft Oy
---
# Part One - Introduction
Rate from one to three (1. beginner, 2. junior, 3. senior) how experienced you are with
a. Domain Driven Design<br/>
b. Functional Programming
Example:
```
a: 1, b: 2
```
---
# Part Two - Theory
* Domain Driven Design
* Software Architectures
* Functional Programming
* Railway Oriented Programming
* Transforming Programming
---
## Domain Driven Design
<img src="./bounded_context.png" alt="" width="70%" />
<br>
<span style="font-size:0.5em;">Copyright 2018 Scott Wlaschin, Domain Modeling Made Functional.</span>
* **Structure and language of software code should match the business domain.**
* Bounded context: context within which a model applies
* E.g Billing and Shipping are probably different contexts
---
### Domain Driven Design - Event pipelines
<img src="./event-pipeline.png" alt="" width="70%" />
<br>
Difference between Event and Command is that Event is an noncancelable fact and Command is something which can fail.
Domain Events are written in the past tense. Commands are written in the imperative.
Example:
* Event: BeerOrdered
* Command: OrderBeer
* Event : BeerDelivered
---
## Object Oriented Architecture
Layer Cake
<img src="./layer_cake.png" alt="" width="65%" />
<br>
<span style="font-size:0.5em;">Copyright 2018 Scott Wlaschin, Domain Modeling Made Functional.</span>
Use cases typically cut through layers.
---
## Architecture, Vertical Slices
Can be improved by splitting flows into vertical slices.
<img src="./vertical_slice.png" alt="" width="40%" />
<br>
<span style="font-size:0.5em;">Copyright 2018 Scott Wlaschin, Domain Modeling Made Functional.</span>
---
## Architecture, Vertical Slices
Still not good.
<img src="./horizontal_pipe.png" alt="" width="100%" />
<br>
<span style="font-size:0.5em;">Copyright 2018 Scott Wlaschin, Domain Modeling Made Functional.</span>
---
## Domain Centric Architectures
### Onion Architecture
<img src="./onion.png" alt="" width="80%" />
<br>
<span style="font-size:0.5em;">Copyright 2018 Scott Wlaschin, Domain Modeling Made Functional.</span>
---
## Domain Centric Architectures
### Clean Architecture
<img src="./clean_architecture.jpg" alt="" width="60%" />
https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html
---
## Domain Centric Architectures
Plus others such as hexagonal.
---
## Architecture, I/O
Push I/O to edges.
<img src="./io.png" alt="" width="100%" />
<br>
<span style="font-size:0.5em;">Copyright 2018 Scott Wlaschin, Domain Modeling Made Functional.</span>
---
## Functional Programming, 101
* Functions
* Functions
* Functions
* Immutability
* Algebraic Data Types (e.g. a type formed by combining other types)
* Product types
* Sum types
* Avoid (observable) side effects (mutable state, exceptions, I/O, etc)
---
### Functional Programming, schools
Very roughly two styles
* Pure
* Exceptionalists
Pure prefer pure functional programming where no side effects are not allowed (use IO monad et al.).
Exceptionalists (or pragmatists) consider exceptions to be exceptional and therefore OK.
---
## Railway Oriented Programming

--

--

https://fsharpforfunandprofit.com/posts/recipe-part2/
and
https://fsharpforfunandprofit.com/posts/against-railway-oriented-programming/
---
## Transforming Programming
```sh
$ find . -type f | xargs wc -l | sort -n | tail -5
```
```javascript
const double = (n) => n * 2;
const increment = (n) => n + 1;
// without pipeline operator
double(increment(double(double(5)))); // 42
// with pipeline operator
5 |> double |> double |> increment |> double; // 42
```
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Pipeline_operator
---
# Part Three - Techniques
---
## Techniques
- Use types to describe domain, e.g. `UnvalidatedPayment` and `PaidInvoice`
- Avoid primitive/generic types like: `Int`, `List` or `String`
```kotlin
id: Int
```
vs
```kotlin
id: UserId
```
- Add abstraction using types, functions, modules, packages, aliases, etc.
---
### OO vs. FP
```kotlin
interface UserService {
fun userExists(id: Int): Boolean
}
```
vs.
```kotlin
typealias UserExists = (Int) -> Boolean
```
Were to put/collect functions? The most important thing is to group them meaningfully. e.g. per (sub)domain or context.
Different languages have different ways to group code e.g.:
* modules
* packages
* objects
* classes
* etc.
---
## Techniques, Kotlin Example
```kotlin
fun validateOrderId(id: Int): Boolean
```
vs.
```kotlin
data class UnvalidatedOrderId(val id: Int)
sealed class OrderId
data class ValidOrderId(val id: Int): OrderId()
data class InvalidOrderId(val id: Int): OrderId()
typealias ValidateOrderId = (UnvalidatedOrderId) -> OrderId
```
---
## Techniques, Scala Example
```scala
def validateOrderId(id: Int): Boolean
```
vs.
```scala
case class UnvalidatedOrderId(id: Int)
sealed trait OrderId
case class ValidOrderId(id: Int) extends OrderId
case class InvalidOrderId(id: Int) extends OrderId
type ValidateOrderId = UnvalidatedOrderId => OrderId
```
---
## Techniques, TypeScript Example
```typescript
function validateOrderId(id: number): boolean
```
vs.
```typescript
type UnvalidatedOrderId = { readonly unvalidatedOrderId: number }
type ValidOrderId = { readonly validOrderId: number }
type InvalidOrderId = { readonly invalidOrderId: number }
type OrderId = ValidOrderId | InvalidOrderId
type ValidateOrderId = (unvalidatedOrderId : UnvalidatedOrderId) => OrderId
```
---
## Techniques, F# Example
```fsharp
let validateOrderId(id: int): bool
```
vs.
```fsharp
type UnvalidatedOrderId = UnvalidatedOrderId of int
type OrderId =
| ValidOrderId of int
| InvalidOrderId of int
type ValidateOrderId = UnvalidatedOrderId -> OrderId
```
---
# Part Four - Example
---
## Example Case: User Borrows a Book
*This is a **design** exercise to explore and refine domain model using types and abstractions.*
*You should defer actual implementation as long as possible.*
---
## Case: user borrows a book, 1
Write minimal amount of code which might describe the use case
```kotlin
data class User()
data class Book()
data class Borrow()
typealias BorrowBook = (User, Book) -> Borrow
```
It doesn't even have to compile.
*Note: this is not how DDD actually works but we just have to start somewhere.*
---
## Case: user borrows a book, 2
Work in small increments. Commit often.
```kotlin
data class User(val id: Int)
data class Book(val id: Int)
data class Borrow(val user: User, val book: Book)
typealias BorrowBook = (User, Book) -> Borrow
```
---
## Case: user borrows a book, 3
Start figuring out different outcomes.
```kotlin
data class User(val id: Int)
data class Book(val id: Int)
sealed class Borrow
data class Borrowed(val user: User, val book: Book): Borrow()
data class BookNotCurrentlyAvailable(val user: User, val book: Book): Borrow()
typealias BorrowBook = (User, Book) -> Borrow
```
---
## Case: user borrows a book, 4
All outcomes behind single `Borrow` type.
```kotlin
data class User(val id: Int)
data class Book(val id: Int)
sealed class Borrow
data class Borrowed(val user: User, val book: Book): Borrow()
data class BookNotCurrentlyAvailable(val user: User, val book: Book): Borrow()
data class BookNotFound(val book: Book): Borrow()
data class UserNotFound(val user: User): Borrow()
typealias BorrowBook = (User, Book) -> Borrow
```
---
## Sidetrack
Railway oriented programming.
```kotlin
sealed class Result<out T, out E>
data class Success<T, E>(val success: T) : Result<T, E>()
data class Failure<T, E>(val error: E) : Result<T, E>()
```
---
## Case: user borrows a book, 5
If we want keep `Success` and `Failure` cases separate
```kotlin
data class User(val id: Int)
data class Book(val id: Int)
data class Borrowed(val user: User, val book: Book)
sealed class BorrowFailure
data class BookNotCurrentlyAvailable(val user: User,
val book: Book): BorrowFailure()
data class BookNotFound(val book: Book): BorrowFailure()
data class UserNotFound(val user: User): BorrowFailure()
typealias BorrowBook = (User, Book) -> Result<Borrowed, BorrowFailure>
```
---
## Case: user borrows a book, 6
How do we get User and Book?
```kotlin
data class UserId(val id: Int)
data class BookId(val id: Int)
sealed class Borrow
data class Borrowed(val user: UserId, val book: BookId): Borrow()
data class BookNotCurrentlyAvailable(val user: UserId, val book: BookId): Borrow()
data class BookNotFound(val book: BookId): Borrow()
data class UserNotFound(val user: UserId): Borrow()
typealias FindUserById = (UserId) -> UserId? // ???
typealias UserExistsById = (UserId) -> Boolean // ???
typealias BorrowBook = (UserId, BookId) -> Borrow?
```
---
## Case: user borrows a book, 7
Keep refining the domain using types.
```kotlin
data class UnvalidatedUserId(val id: Int)
data class UnvalidatedBookId(val id: Int)
data class ValidUserId(val id: Int)
data class ValidBookId(val id: Int)
sealed class Borrow
data class Borrowed(val user: ValidUserId, val book: ValidBookId): Borrow()
data class BookNotCurrentlyAvailable(val user: ValidUserId,
val book: ValidBookId): Borrow()
data class BookNotFound(val book: UnvalidatedBookId): Borrow()
data class UserNotFound(val user: UnvalidatedUserId): Borrow()
typealias ValidateUserId = (UnvalidatedUserId) -> ValidUserId?
typealias ValidateBookId = (UnvalidatedBookId) -> ValidBookId?
typealias BorrowBook =
(ValidateUserId) -> // dependency
(ValidateBookId) -> // dependency
(UnvalidatedUserId, UnvalidatedBookId) -> Borrow?
```
---
## Case: user borrows a book, 8
Fix Event names and add Command.
```kotlin
data class UnvalidatedUserId(val id: Int)
data class UnvalidatedBookId(val id: Int)
data class ValidUserId(val id: Int)
data class ValidBookId(val id: Int)
sealed class Borrowed
data class BookBorrowed(val validUserId: ValidUserId,
val validBookId: ValidBookId) : Borrowed()
data class BookNotFound(val bookId: UnvalidatedBookId) : Borrowed()
data class UserNotFound(val userId: UnvalidatedUserId) : Borrowed()
data class BookNotCurrentlyAvailable(val validUserId: ValidUserId,
val validBookId: ValidBookId) : Borrowed()
data class BorrowBook(val unvalidatedBookId: UnvalidatedBookId,
val unvalidatedUserId: UnvalidatedUserId)
typealias ValidateUserId = (UnvalidatedUserId) -> ValidUserId?
typealias ValidateBookId = (UnvalidatedBookId) -> ValidBookId?
typealias BorrowBookFlow =
(ValidateUserId) -> // dependency
(ValidateBookId) -> // dependency
(BorrowBook) -> Borrowed
```
---
## Case: user borrows a book, 9
Solve how to mark book borrowed. Good enough. We're done 🎉
```kotlin
data class UnvalidatedUserId(val id: Int)
data class UnvalidatedBookId(val id: Int)
data class ValidUserId(val id: Int)
data class ValidBookId(val id: Int)
sealed class Borrowed
data class BookBorrowed(val validUserId: ValidUserId,
val validBookId: ValidBookId) : Borrowed()
data class BookNotFound(val bookId: UnvalidatedBookId) : Borrowed()
data class UserNotFound(val userId: UnvalidatedUserId) : Borrowed()
data class BookNotCurrentlyAvailable(val validUserId: ValidUserId,
val validBookId: ValidBookId) : Borrowed()
data class BorrowBook(val unvalidatedBookId: UnvalidatedBookId,
val unvalidatedUserId: UnvalidatedUserId)
typealias ValidateUserId = (UnvalidatedUserId) -> ValidUserId?
typealias ValidateBookId = (UnvalidatedBookId) -> ValidBookId?
typealias MarkBookBorrowed = (ValidUserId, ValidBookId) -> BookBorrowed?
typealias BorrowBookFlow =
(ValidateUserId, ValidateBookId, MarkBookBorrowed) -> // dependencies
(BorrowBook) -> Borrowed
```
---
## Case: user borrows a book, 10
Implementation.
```kotlin
typealias BorrowBookFlow =
(ValidateUserId, ValidateBookId, MarkBookBorrowed) -> // dependencies
(BorrowBook) -> Borrowed
val borrowBookFlow: BorrowBookFlow =
{ validateUserId, validateBookId, markBookBorrowed ->
{ borrowBook ->
val validUserId = validateUserId(borrowBook.unvalidatedUserId)
val validBookId = validateBookId(borrowBook.unvalidatedBookId)
when {
validUserId == null -> UserNotFound(borrowBook.unvalidatedUserId)
validBookId == null -> BookNotFound(borrowBook.unvalidatedBookId)
else ->
markBookBorrowed(validUserId, validBookId)
?: BookNotCurrentlyAvailable(validUserId, validBookId)
}
}
}
```
---
# Part Five - Coding Exercises
Some suggestions using public library as a playground:
* Extend example case with book return
* Add a book into library catalog
* Validate book info (e.g. name, author, isbn)
* User renews an item
* Item can be renewed up to five times in a row.
* Overdue item notification
* Send notification
* Include accumulated fine
* Lost item handling
* Mark item lost
* Add fine
* Send notification
---
# Part Four - Demos and Discussion
---
class: center, middle
# Thank you!
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create(
);
</script>
</body>
</html>