Slide 1

Slide 1 text

@jenny_codes Context-Driven Development Architect your Code with Phoenix Context

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

https://media.giphy.com/media/NWtFdrEXGwZX2/giphy.gif

Slide 5

Slide 5 text

• Quick onboarding

Slide 6

Slide 6 text

• Quick onboarding • Effective

Slide 7

Slide 7 text

• Quick onboarding • Effective • Using Elixir

Slide 8

Slide 8 text

https://twitter.com/DJTGIF/status/856856284892729345 Context

Slide 9

Slide 9 text

What do I do with contexts?

Slide 10

Slide 10 text

Outline • The cunning challenges of contexts • The quest, and what did not work • Build-your-own architecture • Revisit contexts • Context-driven development

Slide 11

Slide 11 text

Outline • The cunning challenges of contexts • The quest, and what did not work • Build-your-own architecture • Revisit contexts • Context-driven development

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

“Contexts are dedicated modules that expose and group related functionality.“

Slide 14

Slide 14 text

Model Controller View Request Response

Slide 15

Slide 15 text

Model Controller View /your_app_web /your_app

Slide 16

Slide 16 text

Phoenix MVC structure lib ├── your_app ├── your_app_web Model Controller View /your_app_web /your_app

Slide 17

Slide 17 text

Phoenix MVC structure lib ├── your_app ├── your_app_web ├── controllers ├── endpoint.ex ├── router.ex ├── templates └── views Model Controller View /your_app_web /your_app

Slide 18

Slide 18 text

Phoenix MVC structure lib ├── your_app ├── ??? ├── ??? └── ??? ├── your_app_web Model Controller View /your_app_web /your_app

Slide 19

Slide 19 text

Phoenix MVC structure lib ├── your_app ├── ??? ├── ??? └── ??? ├── your_app_web Model Controller View /your_app_web /your_app Context

Slide 20

Slide 20 text

The cunning challenges of contexts

Slide 21

Slide 21 text

The cunning challenges of contexts • Different layers of concerns are mixed together.

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

The cunning challenges of contexts

Slide 25

Slide 25 text

concept of context Ecto business logic juggler by Orin zuu from the Noun Project The cunning challenges of contexts

Slide 26

Slide 26 text

The cunning challenges of contexts • Different layers of concerns are mixed together. • Relationships between components are ambiguous.

Slide 27

Slide 27 text

from Phoenix of fi cial guide “Sometimes it may be tricky to determine if two resources belong to the same context or not. In those cases, prefer distinct contexts per resource and refactor later if necessary. “ The cunning challenges of contexts

Slide 28

Slide 28 text

Outline • The cunning challenges of contexts • The quest, and what did not work • Build-your-own architecture • Revisit contexts • Context-driven development

Slide 29

Slide 29 text

What to do with context? • Look at open-source projects?

Slide 30

Slide 30 text

What to do with context? • Look at open-source projects? • Domain Driven Design?

Slide 31

Slide 31 text

source: https://youtu.be/tMO28ar0lW8

Slide 32

Slide 32 text

https://gfycat.com/elderlyhatefulhornet Challenge accepted.

Slide 33

Slide 33 text

https://www.domainlanguage.com/wp-content/uploads/2016/05/DDD_Reference_2015-03.pdf

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

https://gfycat.com/composedimportantavians

Slide 36

Slide 36 text

💡

Slide 37

Slide 37 text

https://gph.is/g/4zqknpn?tc=1 via @giphy architecture

Slide 38

Slide 38 text

from Phoenix of fi cial guide “Contexts are dedicated modules that expose and group related functionality.“

Slide 39

Slide 39 text

Outline • The cunning challenges of contexts • The quest, and what did not work • Build-your-own architecture • Revisit contexts • Context-driven development

Slide 40

Slide 40 text

How do I build an application?

Slide 41

Slide 41 text

Use case Adapter Infrastructure Domain

Slide 42

Slide 42 text

Adapter Infrastructure Domain Use case • Business logic • Free of technology

Slide 43

Slide 43 text

Use case Adapter Infrastructure Domain • Use case orchestration • Transaction management

Slide 44

Slide 44 text

Use case Adapter Infrastructure Domain • Translation

Slide 45

Slide 45 text

Use case Adapter Infrastructure Domain • External dependency • Technical details

Slide 46

Slide 46 text

Adapter Use case Infrastructure Domain

Slide 47

Slide 47 text

Clean architecture file structure lib ├── domain ├── use_case ├── adapter └── infrastructure

Slide 48

Slide 48 text

Clean architecture file structure lib ├── domain ├── use_case ├── adapter └── infrastructure ├── model ├── store (interface) └── service

Slide 49

Slide 49 text

Clean architecture file structure lib ├── domain ├── use_case ├── adapter └── infrastructure ├── upload_image.ex └── register_user.ex

Slide 50

Slide 50 text

Clean architecture file structure lib ├── domain ├── use_case ├── adapter └── infrastructure ├── cli.ex ├── web └── store

Slide 51

Slide 51 text

Clean architecture file structure lib ├── domain ├── use_case ├── adapter └── infrastructure ├── cli.ex ├── web └── store

Slide 52

Slide 52 text

Clean architecture file structure lib ├── domain ├── use_case ├── adapter └── infrastructure ├── cli.ex ├── web └── store

Slide 53

Slide 53 text

Clean architecture file structure lib ├── domain ├── use_case ├── adapter └── infrastructure ├── application.ex ├── mailer.ex ├── repo.ex ├── web ├── endpoint.ex ├── gettext.ex ├── router.ex └── telemetry.ex

Slide 54

Slide 54 text

(Revisit) The cunning challenges of contexts • Different layers of concerns are mixed together. • Relationships between components are ambiguous.

Slide 55

Slide 55 text

• Different layers of concerns are mixed together. Separation of concerns 👍 • Relationships between components are ambiguous.

Slide 56

Slide 56 text

• Different layers of concerns are mixed together. Separation of concerns 👍 • Relationships between components are ambiguous. Clear relationship between components 👍

Slide 57

Slide 57 text

• Different layers of concerns are mixed together. Separation of concerns 👍 • Relationships between components are ambiguous. Clear relationship between components 👍 • Very testable 👍

Slide 58

Slide 58 text

The almighty clean architecture • Separation of concerns • Clear relationship between components • Very testable

Slide 59

Slide 59 text

https://media.giphy.com/media/jQnrJSmFB6KVQ7kfpK/giphy.gif

Slide 60

Slide 60 text

Outline • The cunning challenges of contexts • The quest, and what did not work • Build-your-own architecture • Revisit contexts • Context-driven development

Slide 61

Slide 61 text

from Phoenix of fi cial guide “Contexts are dedicated modules that expose and group related functionality.“

Slide 62

Slide 62 text

How can context improve our current architecture?

Slide 63

Slide 63 text

Example: User noti fi cation Use case Adapter Infrastructure Domain

Slide 64

Slide 64 text

Adapter Infrastructure Domain Use case • model/noti fi cation.ex • store/noti fi cation_store.ex • store/user_store.ex Example: User noti fi cation

Slide 65

Slide 65 text

Use case Adapter Infrastructure Domain • notify_user.ex Example: User noti fi cation

Slide 66

Slide 66 text

Use case Adapter Infrastructure Domain • store/user_store.ex • store/noti fi cation_store.ex • web/controller/ noti fi cation_controller.ex Example: User noti fi cation

Slide 67

Slide 67 text

Improve current architecture with context • domain/model/noti fi cation.ex • domain/store/noti fi cation_store.ex • domain/store/user_store.ex • use_case/notify_user.ex • adapter/store/noti fi cation_store.ex • adapter/store/user_store.ex • adapter/web/controller/noti fi cation_controller.ex

Slide 68

Slide 68 text

• domain/model/noti fi cation.ex • domain/store/noti fi cation_store.ex • domain/store/user_store.ex • use_case/notify_user.ex • adapter/store/noti fi cation_store.ex • adapter/store/user_store.ex • adapter/web/controller/noti fi cation_controller.ex Improve current architecture with context

Slide 69

Slide 69 text

• domain/model/noti fi cation.ex • domain/model/user.ex • domain/store/noti fi cation_store.ex • domain/store/user_store.ex • use_case/notify_user.ex • adapter/store/noti fi cation_store.ex • adapter/store/user_store.ex • adapter/web/controller/noti fi cation_controller.ex Improve current architecture with context

Slide 70

Slide 70 text

• domain/model/noti fi cation.ex • domain/model/user.ex • domain/store/noti fi cation_store.ex • domain/store/user_store.ex • use_case/notify_user.ex • adapter/store/noti fi cation_store.ex • adapter/store/user_store.ex • adapter/web/controller/noti fi cation_controller.ex Improve current architecture with context

Slide 71

Slide 71 text

Adapter Use case Infrastructure Domain

Slide 72

Slide 72 text

Adapter Use case Infrastructure Domain

Slide 73

Slide 73 text

Apply context Adapter Use case Infrastructure Domain

Slide 74

Slide 74 text

Apply contexts Adapter Use case Infrastructure Domain External Internal

Slide 75

Slide 75 text

Apply contexts Adapter Use case Infrastructure Domain External Internal

Slide 76

Slide 76 text

Apply contexts Internal Use case Domain

Slide 77

Slide 77 text

Apply contexts Internal Use case Domain Context

Slide 78

Slide 78 text

Apply contexts Use case Domain Context YourApp.[Context] YourApp.[Context].[UseCase] YourApp.[Context].[Domain]

Slide 79

Slide 79 text

Apply contexts Use case Domain Context Noti fi cation Noti fi cation.notify_user Noti fi cation.Noti fi cation Noti fi cation.UserStore Noti fi cation.Noti fi cationStore

Slide 80

Slide 80 text

Apply contexts Use case Domain Context Noti fi cation Noti fi cation.notify_user Noti fi cation.Noti fi cation Noti fi cation.UserStore Noti fi cation.Noti fi cationStore

Slide 81

Slide 81 text

Apply contexts Use case Domain Context Noti fi cation Noti fi cation.notify_user Noti fi cation.Noti fi cation Noti fi cation.UserStore Noti fi cation.Noti fi cationStore

Slide 82

Slide 82 text

Apply contexts Use case Domain Context Noti fi cation Noti fi cation.notify_user Noti fi cation.Noti fi cation Noti fi cation.UserStore Noti fi cation.Noti fi cationStore

Slide 83

Slide 83 text

File structure ├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex

Slide 84

Slide 84 text

File structure ├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex # context

Slide 85

Slide 85 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 86

Slide 86 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 87

Slide 87 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 88

Slide 88 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 89

Slide 89 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 90

Slide 90 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 91

Slide 91 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 92

Slide 92 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 93

Slide 93 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 94

Slide 94 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex File structure

Slide 95

Slide 95 text

ElixirLab Noti fi cation Account Cross-context relationship

Slide 96

Slide 96 text

ElixirLab Noti fi cation Account Account.create_user Noti fi cation.not fi y_new_user Cross-context relationship

Slide 97

Slide 97 text

ElixirLab Noti fi cation Account Cross-context relationship: Implicit dependency? depends on

Slide 98

Slide 98 text

ElixirLab Noti fi cation Account Cross-context relationship depends on depends on

Slide 99

Slide 99 text

├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex The external layer

Slide 100

Slide 100 text

elixir_lab ├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex The external layer

Slide 101

Slide 101 text

elixir_lab ├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex elixir_lab_external ├── noti fi cation_store.ex ├── repo │ ├── schema │ └── user_repo.ex ├── web (Phoenix) The external layer

Slide 102

Slide 102 text

elixir_lab ├── noti fi cation │ ├── noti fi cation.ex │ ├── noti fi cation_store.ex │ └── user_store.ex └── noti fi cation.ex elixir_lab_external ├── noti fi cation_store.ex ├── repo │ ├── schema │ └── user_repo.ex ├── web (Phoenix) The external layer

Slide 103

Slide 103 text

• Separation of concerns • Clear relationship between components • Very testable Result of applying context

Slide 104

Slide 104 text

• Separation of concerns • Clear relationship between components • Very testable • Groups related functionality together 👍 Result of applying context

Slide 105

Slide 105 text

Outline • The cunning challenges of contexts • The quest, and what did not work • Build-your-own architecture • Revisit contexts • Context-driven development 🏁

Slide 106

Slide 106 text

Context-driven development • Separate your business concerns.

Slide 107

Slide 107 text

Context-driven development • Separate your business concerns. • Take advantage of Elixir’s fl exibility.

Slide 108

Slide 108 text

Context-driven development • Separate your business concerns. • Take advantage of Elixir’s fl exibility. • But use it carefully.

Slide 109

Slide 109 text

Context-driven development • Separate your business concerns. • Take advantage of Elixir’s fl exibility. • But use it carefully. • Make your architecture scream.

Slide 110

Slide 110 text

source: https://tenor.com/view/chubby-chubby-cat-stuck-door-pet-door-gif-5518148 Phoenix Your application

Slide 111

Slide 111 text

source: http://gph.is/1eeJ98k?tc=1 Phoenix Your application

Slide 112

Slide 112 text

Resources Books • Clean Architecture: A Craftsman's Guide to Software Structure by Robert C. Martin • Functional Web Development with Elixir, OTP, and Phoenix by by Lance Halvorsen • Domain Modeling Made Functional by Scott Wlaschin • Implementing Domain-Driven Design by Vaughn Vernon • Programming Phoenix by Bruce Tate, Chris McCord and José Valim

Slide 113

Slide 113 text

Resources Videos • Building Beautiful Systems with Phoenix Contexts and DDD by Andrew Hao • Lonestar ElixirConf 2017- KEYNOTE: Phoenix 1.3 by Chris McCord • Ruby Midwest 2011 - Keynote: Architecture the Lost Years by Robert Martin

Slide 114

Slide 114 text

Resources Articles / Blog posts / Forum questions • https://hexdocs.pm/phoenix/contexts.html • https://elixirforum.com/t/hexagonal-architecture-in-elixir/32225/16 • https://elixirforum.com/t/how-to-determine-contexts-with-phoenix-1-3/4367 • https://aaronrenner.io/2019/09/18/application-layering-a-pattern-for-extensible- elixir-application-design.html • https://www.destroyallsoftware.com/screencasts/catalog/functional-core- imperative-shell • https://blog.cleancoder.com

Slide 115

Slide 115 text

https://github.com/jenny-codes/elixirlab @jenny_codes