Slide 1

Slide 1 text

@suzushin 5 4 - Shinichiro 
 Go Go Conference mini 2 0 2 2 Autumn IN SENDAI #SendaiGo

Slide 2

Slide 2 text

👨💻 whoami Intro @suzushin 54 - Shinichiro Go 2 C#, PHP SWE@Showcase Gig 2

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

🍔 Intro 5 😋 👩🍳 Order Created Order Received Pickup Time Changed Order Completed Order Cancelled 🤵 Preparation Completed Customer Server Kitchen
 Sta f ஫ จ Ωϟϯηϧ Ω ϟϯ η ϧ ड෇ डऔ࣌ؒมߋ ड ౉ ׬ ྃ ४උ׬ྃ

Slide 6

Slide 6 text

🗣 Intro 😮💨 Learning Domain-Driven Design 📚 OK MSA C# JVM Go 6

Slide 7

Slide 7 text

🙇 Intro 7

Slide 8

Slide 8 text

Intro 📋 Agenda 1 . State Sourcing and Event Sourcing 2 . Go examples 3 . Cyclomatic complexity and Cognitive complexity 4 . Conclusion 8

Slide 9

Slide 9 text

ʰਓ݄ͷਆ࿩ʱʢϑϨσϦοΫɾϒϧοΫεɺୌ୔ప ༁ɺؙળग़൛ɺ2014೥ʣ “ࢲʹϑϩʔνϟʔτ͚ͩΛݟͤͯɺςʔϒϧ͸ݟͤͳ͍ͱͨ͠Βɺ ࢲ͸ͣͬͱԎʹר͔Εͨ··ʹͳΔͩΖ͏ɻٯʹςʔϒϧ͕ݟͤͯ ΋Β͑ΔͳΒɺϑϩʔνϟʔτ͸͍͍ͨͯඞཁͳ͘ͳΔɻͦΕ͚ͩ ͰɺΈΜͳ໌നʹ෼͔ͬͯ͠·͏͔Βͩɻ”

Slide 10

Slide 10 text

State sourcing

Slide 11

Slide 11 text

11 State Sourcing What is State Sourcing Entity CRUD API ID ঎඼ ݸ਺ ঢ়ଶ Version डऔ༧ఆ೔࣌ ࡞੒೔࣌ ߋ৽೔࣌ 1 ಛ্ڇλϯห౰ 2 Complete 4 2022-10-15 09:25:00 2022-10-15 09:00:00 2022-10-15 09:30:00 2 ͣΜͩγΣΠΫ 1 Ready 2 2022-10-15 15:00:00 2022-10-15 10:00:00 2022-10-15 10:15:00 3 Ή͢ͼؙห౰ 2 Create 1 2022-10-15 14:30:00 2022-10-15 10:30:00 2022-10-15 10:30:00 4 ڇλϯห౰ 4 Cancel 5 2022-10-15 13:00:00 2022-10-15 12:00:00 2022-10-15 12:40:00 Orders Order{ id: 4, status: Cancel, orderItem: “ڇλϯห౰”, 
 version: 5, pickupTime: "2022-10-15 13:00:00", orderedAt: "2022-10-15 12:00:00", } Order Entity ͷΠϝʔδ ૬ޓม׵

Slide 12

Slide 12 text

12 State Sourcing Orders Table (Create) (Ready) (Complete) (Cancel) 🤔 ID: 4 ID ঎඼ ݸ਺ ঢ়ଶ Version डऔ༧ఆ೔࣌ ࡞੒೔࣌ ߋ৽೔࣌ 1 ಛ্ڇλϯห౰ 2 Complete 4 2022-10-15 09:25:00 2022-10-15 09:00:00 2022-10-15 09:30:00 2 ͣΜͩγΣΠΫ 1 Ready 2 2022-10-15 15:00:00 2022-10-15 10:00:00 2022-10-15 10:15:00 3 Ή͢ͼؙห౰ 2 Create 1 2022-10-15 14:30:00 2022-10-15 10:30:00 2022-10-15 10:30:00 4 ڇλϯห౰ 4 Cancel 5 2022-10-15 13:00:00 2022-10-15 12:00:00 2022-10-15 12:40:00 Orders

Slide 13

Slide 13 text

Event Sourcing

Slide 14

Slide 14 text

14 Event Sourcing What is Event Sourcing 
 貢 SSOT Figure 7-1ʰLearning Domain-Driven Design: Aligning Software Architecture and Business Strategyʱ
 Vladik Khononov Oreilly & Associates Inc (2021/11/2) Interface ͸͜Μͳײ͡ Update, Delete ͸ແ͍

Slide 15

Slide 15 text

15 Event Sourcing What is Aggregate Eric Evans. (Japanese Edition) (p. 12 5 ). Kindle Edition. ʰϚΠΫϩαʔϏεύλʔϯ: ࣮ફతγεςϜσβΠϯͷͨΊͷίʔυղઆʱ
 Chris Richardson,௕ඌߴ߂,Ḻᖒ޿ږ (2020/03/23) p.174

Slide 16

Slide 16 text

16 Event Sourcing What is Domain Event “υϝΠϯΠϕϯτͱ͸ɺϏδωευϝΠϯͰൃੜͨ͠ॏཁͳΠϕϯτΛهड़ͨ͠
 ϝοηʔδͷ͜ͱͰ͋Δɻ” ʰLearning Domain-Driven Design: Aligning Software Architecture and Business Strategyʱ
 Vladik Khononov Oreilly & Associates Inc (2021/11/2) “DDDͰυϝΠϯΠϕϯτͱ͍͑͹ɺΞάϦήʔτʹى͖ͨԿ͔ͷ͜ͱͰ͢ɻ
 υϝΠϯΠϕϯτ͸ɺυϝΠϯϞσϧͷΫϥεʹΑͬͯදݱ͞Ε·͢ɻ ” ʰϚΠΫϩαʔϏεύλʔϯ: ࣮ફతγεςϜσβΠϯͷͨΊͷίʔυղઆʱ
 Chris Richardson,௕ඌߴ߂,Ḻᖒ޿ږ (2020/03/23) p.178

Slide 17

Slide 17 text

17 Event Sourcing Example of Order context 1. Order Created 😋 υϝΠϯ
 Πϕϯτ

Slide 18

Slide 18 text

18 Event Sourcing Example of Order context 1. Order Created 😋 🤵 2. Order Received ௥ه͍ͯ͘͠

Slide 19

Slide 19 text

19 Event Sourcing Example of Order context 2 3. Pickup Time Changed 4. Pickup Time Changed 5. Order Cancelled 1. Order Created 😋 2. Order Received 🤵 👩🍳 😡 ͚͍ܽͯͨετʔϦʔ͕ݟ͑ͯ͘Δ

Slide 20

Slide 20 text

How to Restore Aggregate ? Event Sourcing 20 Order{ id: 4, status: Cancel, orderItem: “ڇλϯห౰”, 
 version: 5, pickupTime: "2022-10-15 13:00:00", orderedAt: "2022-10-15 12:00:00", } 1 . Order structure 2 . 1 3 .

Slide 21

Slide 21 text

Pros 😄 Event Sourcing Time traveling Deep insight Audit log Advanced optimistic Concurrency management 
 21 7MBEJL,IPOPOPWʰ-FBSOJOH%PNBJO%SJWFO%FTJHO"MJHOJOH4PGUXBSF"SDIJUFDUVSFBOE#VTJOFTT4USBUFHZʱ0SFJMMZ"TTPDJBUFT*OD

Slide 22

Slide 22 text

Cons 🫤 Event Sourcing Learning curve Evolving the model Architectural complexity 22 7MBEJL,IPOPOPWʰ-FBSOJOH%PNBJO%SJWFO%FTJHO"MJHOJOH4PGUXBSF"SDIJUFDUVSFBOE#VTJOFTT4USBUFHZʱ0SFJMMZ"TTPDJBUFT*OD

Slide 23

Slide 23 text

Go examples

Slide 24

Slide 24 text

Sample system Go examples Controller UseCase Domain Infrastructure Layered Architecture HTTP Request Repository pattern (DIP) MySQL 
 24 MySQL 📱 ίʔυ͸ͪ͜Βʹ্͛ͯ͋Γ·͢ɻ https://github.com/suzushin54/event-sourcing-with-go

Slide 25

Slide 25 text

UseCase: OrderCancel State Sourcing Go examples 25 UseCase Controller UseCase Domain Infrastructure EntityΛऔಘ

Slide 26

Slide 26 text

UseCase: OrderCancel State Sourcing Go examples 26 UseCase Controller UseCase Domain Infrastructure ΩϟϯηϧΛ࣮ߦ

Slide 27

Slide 27 text

UseCase: OrderCancel State Sourcing Go examples 27 UseCase Controller UseCase Domain Infrastructure ӬଓԽ

Slide 28

Slide 28 text

UseCase: OrderCancel Event Sourcing Go examples 28 Controller UseCase Domain Infrastructure UseCase EventsΛऔಘ

Slide 29

Slide 29 text

UseCase: OrderCancel Event Sourcing Go examples 29 Controller UseCase Domain Infrastructure UseCase ू໿Λ෮ݩ

Slide 30

Slide 30 text

OrderCreatedEvent Go examples 30 Domain Controller UseCase Domain Infrastructure

Slide 31

Slide 31 text

Entity Go examples 31 Domain Controller UseCase Domain Infrastructure (1) (2)

Slide 32

Slide 32 text

Entity Go examples 32 Domain Controller UseCase Domain Infrastructure (2) (3)

Slide 33

Slide 33 text

UseCase: OrderCancel Event Sourcing Go examples 33 Controller UseCase Domain Infrastructure UseCase Ωϟϯηϧ
 ίϚϯυΛ࡞੒

Slide 34

Slide 34 text

What is Command? Go examples 34 CQRS Documents by Greg Young. http://www.minato.tv/cqrs/cqrs_documents_jp.pdf p. 11 ίϚϯυͱΠϕϯτͷҧ͍

Slide 35

Slide 35 text

Go examples 35 Controller UseCase Domain Infrastructure Domain

Slide 36

Slide 36 text

UseCase: OrderCancel Event Sourcing Go examples 36 Controller UseCase Domain Infrastructure UseCase ίϚϯυΛ࣮ߦ

Slide 37

Slide 37 text

Go examples 37 Controller UseCase Domain Infrastructure Domain ৚݅νΣοΫ Πϕϯτ࡞੒ append

Slide 38

Slide 38 text

UseCase: OrderCancel Event Sourcing Go examples 38 Controller UseCase Domain Infrastructure UseCase ӬଓԽ

Slide 39

Slide 39 text

Go examples Overload 39 https://github.com/vladikk/learning-ddd/blob/main/listings/07-06.cs https://github.com/vladikk/learning-ddd/blob/main/listings/07-07.cs

Slide 40

Slide 40 text

Overloading Go examples Go - - Go - 40 Why does Go not support overloading of methods and operators? Method dispatch is simpli fi ed if it doesn't need to do type matching as well. Experience with other languages told us that having a variety of methods with the same name but di ff erent signatures was occasionally useful but that it could also be confusing and fragile in practice. Matching only by name and requiring consistency in the types was a major simplifying decision in Go's type system. Regarding operator overloading, it seems more a convenience than an absolute requirement. Again, things are simpler without it. 
 https://go.dev/doc/faq#overloading

Slide 41

Slide 41 text

Cyclomatic complexity and Cognitive complexity

Slide 42

Slide 42 text

Overview Cyclomatic complexity and Cognitive Complexity 42 Cyclomatic complexity 1976 Cognitive Complexity 2016 Software metric 1 Fortran developed by SonarSource. 
 if, for +1 
 +1 https://docs.codeclimate.com/docs/cognitive-complexity

Slide 43

Slide 43 text

Go Conference 2 0 2 2 Spring Online Go Conference 2022 Spring Online https://gocon.jp/ 2022 spring/ https://www.youtube.com/watch?v=GZCr-Dbi 0 YM&t= 814 s 13 : 3 4 [A 1 -C] Cognitive Complexity go/ast 43

Slide 44

Slide 44 text

Cyclomatic complexity and Cognitive Complexity 44 https://pkg.go.dev/github.com/uudashr/gocognit#section-readme ܦ࿏ͷ਺͚ͩՃࢉ ωετΛߟྀ

Slide 45

Slide 45 text

Score Cyclomatic complexity and Cognitive Complexity Interpretation In his presentation 'Software Quality Metrics to Identify Risk'[ 8 ] for the Department of Homeland Security, Tom McCabe introduces the following categorisation to interpret cyclomatic complexity: • 1 - 10 Simple procedure, little risk • 1 1 - 20 More complex, moderate risk • 2 1 - 50 Complex, high risk • > 50 Untestable code, very high risk 45 https://en.wikipedia.org/wiki/Cyclomatic_complexity

Slide 46

Slide 46 text

https://github.com/uudashr/gocognit Cognitive Complexity Gocognit Go 
 46 $ go install github.com/uudashr/gocognit/cmd/gocognit@latest # e.g. $ gocognit . $ gocognit main.go $ gocognit -top 10 src/ $ gocognit -over 25 docker $ gocognit -avg . https://github.com/golangci/golangci-lint

Slide 47

Slide 47 text

https://github.com/uudashr/gocognit Cognitive Complexity 47 $ gocognit -top 5 ./app 5 aggregate (*Order).Execute app/domain/aggregate/order.go:94:1 5 usecase (*orderUseCaseV1).ReceiveV1 app/usecase/receive_v1.go:16:1 5 vo (OrderItem).Equals app/domain/vo/order_item.go:26:1 5 usecase (*orderUseCaseV1).CancelV1 app/usecase/cancel_v1.go:15:1 4 controller (*OrderController).CancelOrderV2 app/controller/cancel_order_v2.go:13:1 Sample system v 1 (StateSourcing) v 2 (EventSourcing) ES

Slide 48

Slide 48 text

48 Controller UseCase Domain Infrastructure Domain Cognitive Complexity // + 1 // + 2 (nesting = 1 ) // + 2 (nesting = 1 ) // Cognitive Complexity = 5

Slide 49

Slide 49 text

49 Controller UseCase Domain Infrastructure Domain // + 1 // Cognitive Complexity = 2 // + 1 Execute: 5 → 1 ExecuteCancel: 2 શମͷೝ஌తෳࡶ౓ΛԼ͛Δ͜ͱ͕Ͱ͖Δ Cognitive Complexity

Slide 50

Slide 50 text

Conclusion Pros/Cons Go 💪 50

Slide 51

Slide 51 text

Appendix

Slide 52

Slide 52 text

Projection Logic ReadModel Projection Logic 52

Slide 53

Slide 53 text

Snapshot CQRS https://docs.google.com/presentation/d/ 1 YKL 0 UPIDLN 34 A 1 REB 4 l 7 omsXv 9 f 4 hmmcvL 4 fdwVXweA/ edit#slide=id.g 13 e 14442 5 65 _ 0 _ 3 5 5 53

Slide 54

Slide 54 text

CQRS DB Materialized view 54

Slide 55

Slide 55 text

EventStore RDB Event Store 55 MicrosoftSQLServer Oracle MySQL NoSQL MongoDB RavenDB Azure Blob . (Japanese Edition) (p. 52 5 ). Kindle Edition.