Slide 1

Slide 1 text

Aligning Bounded Contexts with Subdomains in Legacy Code NDC Oslo 2021 Mufrid Krilic Domain-Driven Design Coach www.cowork.no

Slide 2

Slide 2 text

Training and Coaching • Agile Leadership • “Tight-Loose-Tight” • Domain-Driven Design with NDC Workshops • bit.ly/domainstorytelling-online • Blog • https://mufridk.medium.com

Slide 3

Slide 3 text

Book recommendation #1

Slide 4

Slide 4 text

Strategic Domain-Driven Design The Principles

Slide 5

Slide 5 text

Strategic DDD • Focusing on Core domain • Problem and solution space • Subdomains and Bounded Contexts • Linguistic boundaries • Ubiquitous Language • The two pillars of DDD

Slide 6

Slide 6 text

Core Domain • The thing that distinguishes you from the competitors • “Not every part of the system will be well-designed” • Generic subdomain • Supporting subdomain

Slide 7

Slide 7 text

Problem Space and Solution Space Problem space • Domain analysis to discover inherent Subdomains Solution space • Model your application accordingly in multiple Bounded Contexts

Slide 8

Slide 8 text

Linguistic Boundaries Ubiquitous Language • The same language everywhere • Conversations • Documentation • Code

Slide 9

Slide 9 text

The Two Pillars of DDD 1. Ubiquitous Language 2. Bounded Context Bounded contexts in your application are defined by linguistic boundaries

Slide 10

Slide 10 text

Book recommendation #2

Slide 11

Slide 11 text

Boundaries Why?

Slide 12

Slide 12 text

Making Sense of the Problem Space • Aligning with the business • Reducing the translation effort • Ubiquitous Language

Slide 13

Slide 13 text

Reducing the Cognitive Load • Legacy Code • Being able to navigate • …. and understand • Hidden boundaries in legacy code • Look for subdomains

Slide 14

Slide 14 text

Book recommendation #3

Slide 15

Slide 15 text

Boundaries Where?

Slide 16

Slide 16 text

Observable boundaries • Technical • Network • Containers • Organizational • Teams • Departments • Easily observable boundaries

Slide 17

Slide 17 text

Not so easily observable? • The “4+1” View Model of Software Architecture • Philippe Kruchten • https://bit.ly/4plus1Paper

Slide 18

Slide 18 text

https://bit.ly/4plus1Paper

Slide 19

Slide 19 text

Dependencies What are they?

Slide 20

Slide 20 text

Logical View Dependencies The reason for increased coupling • The amount of data that is passed across the boundaries

Slide 21

Slide 21 text

Cost of Dependencies in a Distributed System • Distributed logical dependencies • Across software and process views • Across the organization • Microservices

Slide 22

Slide 22 text

The Story Begins…. Story about a team dealing with legacy system

Slide 23

Slide 23 text

Business Case • Physicians prescribe medications • Integration with systems for prescription delivery in pharmacies • Focus on putting patient needs and safety on the top

Slide 24

Slide 24 text

Use Case Prescribing pre-approved medication

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Drawn with egon.io

Slide 30

Slide 30 text

Use Case Prescribing non-approved medication

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Drawn with egon.io

Slide 38

Slide 38 text

Book recommendation #4

Slide 39

Slide 39 text

Looking for Subdomains .... in Legacy Environments

Slide 40

Slide 40 text

Subdomains in Problem Space • Pivotal Event • NonApprovedMedicationPrescribed • Time-related activity • While waiting for approval the physician stops the prescription process

Slide 41

Slide 41 text

Drawn with egon.io

Slide 42

Slide 42 text

Traces of Bounded Contexts in Development View • Are there any established boundaries in the Development View? • 4+1 Model • .NET projects • Namespaces

Slide 43

Slide 43 text

Dependencies in Logical View • Analysis of • using statements • .NET project references • NuGet package references • Http calls to remote services • Event-based communication with remote services

Slide 44

Slide 44 text

Bounded Contexts …. and Context Mapping

Slide 45

Slide 45 text

DDD Context Mapping Downstream: depending on upstream Upstream: depended on downstream Conformist: Upstream model referenced directly in Downstream context

Slide 46

Slide 46 text

Bounded Contexts in Legacy Code • Respect existing dependencies and relationships • Explicit decision on the amount of work needed to break things up

Slide 47

Slide 47 text

Decision Time

Slide 48

Slide 48 text

Application Bounded Context • Establish observable boundary • Visual Studio Solution • Prescription Bounded Context remains as is • 1 week time-box • Summarize lessons learned regardless of the outcome

Slide 49

Slide 49 text

Step One Establish Visual Studio Solution

Slide 50

Slide 50 text

Move existing .NET projects • Between solutions within the same repository • git mv • Exclude/Include Project Code compiles and unit tests run

Slide 51

Slide 51 text

Integration tests? • Valuable enough feedback with unit tests only? • Integration/E2E tests • Harder to maintain • Can provide runtime feedback

Slide 52

Slide 52 text

Work on E2E tests? • Pros • Early feedback on bounded context/subdomain alignment • Cons • Considerable work needed just to get the tests running • Could actually slow down the feedback loop Focus on unit tests only at this stage

Slide 53

Slide 53 text

Step Two Cleaning up obsolete references

Slide 54

Slide 54 text

Made obsolete in step one • .NET project references • using statements Uncovers hidden dependencies

Slide 55

Slide 55 text

Classes and interfaces - Defined in Application BC - Referenced in Prescription BC

Slide 56

Slide 56 text

Stable Abstraction Principle Packages that are maximally stable should be maximally abstract. • Extract common type definitions to a separate module

Slide 57

Slide 57 text

DDD Context Mapping Shared Kernel: Two contexts share a subset of the domain model

Slide 58

Slide 58 text

Step 3 Make E2E tests to run

Slide 59

Slide 59 text

The end of the story…. • Some E2E tests in Prescription context required DLLs from Application context • Why?

Slide 60

Slide 60 text

Valid business reason for Prescription BC to refer to Application BC - Beware of Circular Dependencies!

Slide 61

Slide 61 text

Business requirement unlocked • As a physician while prescribing a non-approved medicament • I need to see prior applications for the same medicament • To be able to prescribe non-approved medicaments on a life-long basis if required by patient condition Requires dependency from Prescription BC to Application BC

Slide 62

Slide 62 text

Can we avoid circular dependencies? • 4+1 model • Deployment dependencies ok • Circular logical dependencies • All the integration tests in one module • Referencing different bounded contexts • All DLLs would be available

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

View Model Composition • Composing data from different Bounded Contexts on front-end • Suggested reading • Mauro Servienti • https://milestone.topics.it/ • The physician needs only to see the prior applications • Data shown for decision-support

Slide 65

Slide 65 text

Takeaways for the Team ▪ Regular Architectural Reviews – continuous learning about subdomain boundaries ▪ Stable Abstraction Principle – Define desired relationships between the modules ▪ Discover Logical Dependencies in Code Review – 4+1 Model ▪ “You can always make things more cohesive” – Kent Beck Ella Fitzegerald – «They Cant’t Take That Away From Me»

Slide 66

Slide 66 text

Book recommendation #5

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Photos • By !Original: MddVector: Wikimpan - Based on File:4+1 Architectural View Model.jpg by User:Mdd, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=50144028 • https://unsplash.com/@renemolenkamp • https://unsplash.com/@jancanty • https://unsplash.com/@afafa • https://unsplash.com/@walkator • https://unsplash.com/@woutvanacker • https://unsplash.com/@polarmermaid • https://unsplash.com/@cdd20 • https://unsplash.com/@roaming_angel • https://unsplash.com/@micheile • https://unsplash.com/@patrickperkins • https://unsplash.com/@melany_tuinfosalud • https://unsplash.com/@timmossholder