Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

認知的複雑度から見るGo言語のイベントソーシング実装 / Event Sourcing wi...

suzushin54
October 15, 2022

認知的複雑度から見るGo言語のイベントソーシング実装 / Event Sourcing with Go

Go Conference mini 2022 Autumn IN SENDAI の登壇資料です。

suzushin54

October 15, 2022
Tweet

More Decks by suzushin54

Other Decks in Programming

Transcript

  1. @suzushin 5 4 - Shinichiro 
 Go Go Conference mini

    2 0 2 2 Autumn IN SENDAI #SendaiGo
  2. 3

  3. 4

  4. 🍔 Intro 5 😋 👩🍳 Order Created Order Received Pickup

    Time Changed Order Completed Order Cancelled 🤵 Preparation Completed Customer Server Kitchen
 Sta f ஫ จ Ωϟϯηϧ Ω ϟϯ η ϧ ड෇ डऔ࣌ؒมߋ ड ౉ ׬ ྃ ४උ׬ྃ
  5. Intro 📋 Agenda 1 . State Sourcing and Event Sourcing

    2 . Go examples 3 . Cyclomatic complexity and Cognitive complexity 4 . Conclusion 8
  6. 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 ͷΠϝʔδ ૬ޓม׵
  7. 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
  8. 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 ͸ແ͍
  9. 15 Event Sourcing What is Aggregate Eric Evans. (Japanese Edition)

    (p. 12 5 ). Kindle Edition. ʰϚΠΫϩαʔϏεύλʔϯ: ࣮ફతγεςϜσβΠϯͷͨΊͷίʔυղઆʱ
 Chris Richardson,௕ඌߴ߂,Ḻᖒ޿ږ (2020/03/23) p.174
  10. 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
  11. 18 Event Sourcing Example of Order context 1. Order Created

    😋 🤵 2. Order Received ௥ه͍ͯ͘͠
  12. 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 🤵 👩🍳 😡 ͚͍ܽͯͨετʔϦʔ͕ݟ͑ͯ͘Δ
  13. 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 .
  14. 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 
  15. Cons 🫤 Event Sourcing Learning curve Evolving the model Architectural

    complexity 22 7MBEJL,IPOPOPWʰ-FBSOJOH%PNBJO%SJWFO%FTJHO"MJHOJOH4PGUXBSF"SDIJUFDUVSFBOE#VTJOFTT4USBUFHZʱ0SFJMMZ"TTPDJBUFT*OD 
  16. 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
  17. UseCase: OrderCancel Event Sourcing Go examples 33 Controller UseCase Domain

    Infrastructure UseCase Ωϟϯηϧ
 ίϚϯυΛ࡞੒
  18. What is Command? Go examples 34 CQRS Documents by Greg

    Young. http://www.minato.tv/cqrs/cqrs_documents_jp.pdf p. 11 ίϚϯυͱΠϕϯτͷҧ͍
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 48 Controller UseCase Domain Infrastructure Domain Cognitive Complexity // +

    1 // + 2 (nesting = 1 ) // + 2 (nesting = 1 ) // Cognitive Complexity = 5
  26. 49 Controller UseCase Domain Infrastructure Domain // + 1 //

    Cognitive Complexity = 2 // + 1 Execute: 5 → 1 ExecuteCancel: 2 શମͷೝ஌తෳࡶ౓ΛԼ͛Δ͜ͱ͕Ͱ͖Δ Cognitive Complexity
  27. 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
  28. EventStore RDB Event Store 55 MicrosoftSQLServer Oracle MySQL NoSQL MongoDB

    RavenDB Azure Blob . (Japanese Edition) (p. 52 5 ). Kindle Edition.