SlideShare a Scribd company logo
JUST POST IT!
CH4 - PRACTICE
TEXT
BEFORE WE START, YOU CAN CHECK THESE REFERENCES
▸ Java


▸ https://github.com/humank/EventStormingWorkShop


▸ https://github.com/VaughnVernon/IDDD_Samples


▸ https://github.com/ddd-by-examples/library


▸ C#


▸ https://github.com/ArthurChang01/DDDTW.CoffeeShop


▸ https://github.com/VaughnVernon/IDDD_Samples_NET


▸ https://www.wiley.com/go/domaindrivendesign Ch14, Ch15


▸ TypeScript


▸ https://github.com/stemmlerjs/ddd-forum


▸ Php


▸ https://github.com/justericgg/dddtw-laravel-coffee-shop
TEXT
ABOUT ME
▸ Fong


▸ Backend Engineer at Carousell


▸ DDDTW Speaker, Volunteer, Organizer


▸ IT 30-day blogs contest


▸ Think in GraphQL


▸ Think in DDD
OVERVIEW
▸ Value Object Practice


▸ Entity Practice


▸ Aggregate Root Practice


▸ Application Service


▸ Repository
VALUE OBJECT
PRACTICE
ALWAYS NEW
VALUE OBJECT
VALUE OBJECT KEY PROPERTIES
▸ Immutability


▸ Java:
fi
nal or @Value


▸ Java16: Record


▸ C#: readonly


▸ JS/TS: Object.freeze


▸ Attributed-Based Equality


▸ Java: @EqualsAndHashCode


▸ C#:


▸ JS/TS


▸ Replacibility (可替換性): always return new instances in state-changing methods
VALUE OBJECT
HOW TO CREATE VALUE OBJECTS IN VALID STATE (SELF-VALIDATING)
▸ Validate in the constructor


▸ Validate (or partial validate) in Factory method (useful when dealing with
different situations)


▸ Use code contracts to improve readability (tradeoff tech complexity)
VALUE OBJECT
VALID VALUE OBJECT CREATION - 1
▸ Validate in the constructor (before/after assignation)
VALUE OBJECT
VALID VALUE OBJECT CREATION - 2
▸ Validate (or partial validate) in Factory method (useful when dealing with
different situations)
VALUE OBJECT
VALID VALUE OBJECT CREATION - 3
▸ Use code contracts to improve readability (tradeoff tech complexity)
GET YOUR HANDS DIRTY👨💻
VALUE OBJECT
CODE YOUR FIRST VALUE OBJECT - MONEY OBJECT!
▸ Properties


▸ Currency (enum TWD, USD, JPY), Amount


▸ Invariants:


▸ 0 < Amount < INT_MAX


▸ Currency is not null


▸ Methods:


▸ Add(Money m): Money


▸ Subtract(Money m) Money


▸ Factories


▸ Static ofUSD(decimal amount): Money


▸ Static ofTWD(decimal amount): Money
https://github.com/tdd-best/tdd-by-example-the-money/blob/master/src/main/java/money/Money.java
VALUE OBJECT
CODE YOUR FIRST VALUE OBJECT - MONEY OBJECT! PART 1
▸ Properties


▸ Currency (enum TWD, USD, JPY)


▸ Amount


▸ Invariants:


▸ 0 < Amount < 1M


▸ Currency is not null


▸ Test Cases


▸ 10 TWD = 10 TWD


▸ 10 TWD != 20 TWD


▸ 10 TWD != 10 USD


▸ Hints


▸ Properties are immutable (java 16: record, java:
fi
nal, @value, c#: readonly, ts: readonly)


▸ Override equals function
VALUE OBJECT
CODE YOUR FIRST VALUE OBJECT - MONEY OBJECT! PART 2
▸ Methods:


▸ Add (Money augend) -> Money


▸ Cannot add money in different currency


▸ The result cannot be over 1 M


▸ Subtract (Money subtrahend) -> Money


▸ Cannot subtract money in different currency


▸ The result cannot be lower and equal to 0


▸ Test Cases


▸ 10 TWD + 10 TWD = 20 TWD


▸ 10 TWD + 20 USD -> Exception!


▸ 10 TWD - 5 TWD = 5 TWD


▸ 10 TWD - 1 USD -> Exception!


▸ 10 TWD - 20 TWD -> Exception!
https://github.com/tdd-best/tdd-by-example-the-money/blob/master/src/main/java/money/Money.java
VALUE OBJECT
CODE YOUR FIRST VALUE OBJECT - MONEY OBJECT! PART 3
▸ Factories


▸ Static ofUSD(decimal amount): Money


▸ Static ofTWD(decimal amount): Money


▸ Static ofJYP(decimal amount): Money
https://github.com/tdd-best/tdd-by-example-the-money/blob/master/src/main/java/money/Money.java
GOOD JOB!


NEXT ROUND
VALUE OBJECT
POSTDATA
▸ Location: domain/model/PostData


▸ PostData


▸ Properties


▸ Subject


▸ Subject cannot be empty


▸ Body


▸ Body should below 100k words


▸ Methods:


▸ Change(Subject, Body): PostData
https://github.com/tdd-best/tdd-by-example-the-money/blob/master/src/main/java/money/Money.java
ENTITY PRACTICE
KEEP TRACKING
ENTITY
ENTITY KEY PROPERTIES
▸ Must have an ID that can be tracked down the path, and the ID is unique and
cannot be changed


▸ Usually has states


▸ Methods with rich behaviors


▸ Public getter/private setter
ENTITY
PROBLEMS WHEN YOU CREATE AN ENTITY
▸ How to make ID more explicit?


▸ When to generate the Entity ID?


▸ How to make sure
GET YOUR HANDS DIRTY👨💻
ENTITY
POST
▸ Location: domain/model/Post


▸ Properties


▸ ForumId: class ForumId (id: int)


▸ ID: class PostID (id: int)


▸ PostData


▸ creationDate


▸ modi
fi
edDate


▸ Public getter/private setter
ENTITY
POST FACTORY
▸ Static publishPost(forumId, postId, postData) -> Post
THEN, WHO’S THE AGGREGATE ROOT?
AGGREGATE ROOT &
REPOSITORY PRACTICE
ENTRANCE FOR ALL
AGGREGATE ROOT
AGGREGATE ROOT KEY PROPERTIES
▸ Must have an Entity acting as an aggregate root


▸ You can only use aggregate root to update the objects inside the aggregate
root
GET YOUR HANDS DIRTY👨💻
AGGREGATE & REPOSITORY
POST AS AGGREGATE ROOT
▸ Requirements:


▸ updateContent(PostData): void
APPLICATION SERVICE
THE CONDUCTOR
Aggregate Root
Repository
External Service
GET YOUR HANDS DIRTY👨💻
APPLICATION SERVICE
PUBLISH NEW POST
▸ Location: application/PostApplicationSvc


▸ Class PostApplicastionSvc


▸ publishNewPost(String subject, String body, int forumId)


▸ Post post = Post.publishPost(… … …. );


▸ You can just assign a random number to id for now
APPLICATION SERVICE
WHAT DO WE NEED NEXT?
▸ ID generator to encapsulate the implementation detail


▸ Persistent the aggregate root
REPOSITORY
REPOSITORY
REPOSITORY KEY PROPERTIES
▸ Relate to aggregate root, one aggregate root per repository


▸ Only create new methods when you need it in business!!!!


▸ Repository Interface in domain layer, Repository Implements in infra layer
GET YOUR HANDS DIRTY👨💻
REPOSITORY
POST REPOSITORY
▸ Location: domain/model/PostRepository


▸ Interface PostRepository


▸ nextId() -> PostId


▸ save(Post) -> void


▸ Location: infrastructure/persistence/InMemoryPostRepository


▸ Class InMemoryPostRepository implements PostRepository


▸ HashMap<PostId, Post> maps


▸ nextId() -> PostId


▸ save(Post) -> void
REPOSITORY
BACK TO APPLICATION SERVICES
▸ Id = postRepo.nextId();


▸ Post = Post.publishPost(id)


▸ postRepo.save(post)
THANKS
TEXT
REFERENCES
▸ Implementing Domain-Driven Design


▸ Practice, Pattern, Principle of Domain-Driven Design


▸ https://github.com/tdd-best/tdd-by-example-the-money/tree/master/src/main/
java/money


▸ https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-writing-
nested-tests/


▸ https://softwareengineering.stackexchange.com/questions/367145/is-the-
builder-pattern-applicable-in-domain-driven-design

More Related Content

What's hot

React for Beginners
React for BeginnersReact for Beginners
React for Beginners
Derek Willian Stavis
 
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard InterfacesDecoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
Thomas Weinert
 
Your code is not a string
Your code is not a stringYour code is not a string
Your code is not a string
Ingvar Stepanyan
 
The road to continuous deployment (PHPCon Poland 2016)
The road to continuous deployment (PHPCon Poland 2016)The road to continuous deployment (PHPCon Poland 2016)
The road to continuous deployment (PHPCon Poland 2016)
Michiel Rook
 
Groovy Powered Clean Code
Groovy Powered Clean CodeGroovy Powered Clean Code
Groovy Powered Clean Code
noamt
 
Things I Believe Now That I'm Old
Things I Believe Now That I'm OldThings I Believe Now That I'm Old
Things I Believe Now That I'm Old
Ross Tuck
 
Rust ⇋ JavaScript
Rust ⇋ JavaScriptRust ⇋ JavaScript
Rust ⇋ JavaScript
Ingvar Stepanyan
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016
Kacper Gunia
 
Command Bus To Awesome Town
Command Bus To Awesome TownCommand Bus To Awesome Town
Command Bus To Awesome Town
Ross Tuck
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
Kacper Gunia
 
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby DeveloperVenturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Jon Kruger
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
Michael Girouard
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Tsuyoshi Yamamoto
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
XSolve
 
Testable, Object-Oriented JavaScript
Testable, Object-Oriented JavaScriptTestable, Object-Oriented JavaScript
Testable, Object-Oriented JavaScript
Jon Kruger
 
The state of your own hypertext preprocessor
The state of your own hypertext preprocessorThe state of your own hypertext preprocessor
The state of your own hypertext preprocessor
Alessandro Nadalin
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
tutorialsruby
 
What is systemd? Why use it? how does it work? - devoxx france 2017
What is systemd? Why use it? how does it work? - devoxx france 2017What is systemd? Why use it? how does it work? - devoxx france 2017
What is systemd? Why use it? how does it work? - devoxx france 2017
Quentin Adam
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
Mark Baker
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
rajivmordani
 

What's hot (20)

React for Beginners
React for BeginnersReact for Beginners
React for Beginners
 
Decoupling Objects With Standard Interfaces
Decoupling Objects With Standard InterfacesDecoupling Objects With Standard Interfaces
Decoupling Objects With Standard Interfaces
 
Your code is not a string
Your code is not a stringYour code is not a string
Your code is not a string
 
The road to continuous deployment (PHPCon Poland 2016)
The road to continuous deployment (PHPCon Poland 2016)The road to continuous deployment (PHPCon Poland 2016)
The road to continuous deployment (PHPCon Poland 2016)
 
Groovy Powered Clean Code
Groovy Powered Clean CodeGroovy Powered Clean Code
Groovy Powered Clean Code
 
Things I Believe Now That I'm Old
Things I Believe Now That I'm OldThings I Believe Now That I'm Old
Things I Believe Now That I'm Old
 
Rust ⇋ JavaScript
Rust ⇋ JavaScriptRust ⇋ JavaScript
Rust ⇋ JavaScript
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016
 
Command Bus To Awesome Town
Command Bus To Awesome TownCommand Bus To Awesome Town
Command Bus To Awesome Town
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
 
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby DeveloperVenturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
Venturing Into The Wild: A .NET Developer's Experience As A Ruby Developer
 
The Beauty of Java Script
The Beauty of Java ScriptThe Beauty of Java Script
The Beauty of Java Script
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
 
PHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolvePHPCon 2016: PHP7 by Witek Adamus / XSolve
PHPCon 2016: PHP7 by Witek Adamus / XSolve
 
Testable, Object-Oriented JavaScript
Testable, Object-Oriented JavaScriptTestable, Object-Oriented JavaScript
Testable, Object-Oriented JavaScript
 
The state of your own hypertext preprocessor
The state of your own hypertext preprocessorThe state of your own hypertext preprocessor
The state of your own hypertext preprocessor
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
What is systemd? Why use it? how does it work? - devoxx france 2017
What is systemd? Why use it? how does it work? - devoxx france 2017What is systemd? Why use it? how does it work? - devoxx france 2017
What is systemd? Why use it? how does it work? - devoxx france 2017
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 

Similar to 黑豹 ch4 ddd pattern pracrice

Effective codereview | Dave Liddament | CODEiD
Effective codereview | Dave Liddament | CODEiDEffective codereview | Dave Liddament | CODEiD
Effective codereview | Dave Liddament | CODEiD
CODEiD PHP Community
 
War between Tools and Design 2016
War between Tools and Design 2016War between Tools and Design 2016
War between Tools and Design 2016
Mark Windholtz
 
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
VictorSzoltysek
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
André Pitombeira
 
To AWS with Ansible
To AWS with AnsibleTo AWS with Ansible
To AWS with Ansible
☁️ Gerben Geijteman
 
Kotlin初體驗
Kotlin初體驗Kotlin初體驗
Kotlin初體驗
哲偉 楊
 
Introduction to Continous Integration with WordPress
Introduction to Continous Integration with WordPressIntroduction to Continous Integration with WordPress
Introduction to Continous Integration with WordPress
Seagyn Davis
 
Spark core
Spark coreSpark core
Spark core
Freeman Zhang
 
The road to continuous deployment (DomCode September 2016)
The road to continuous deployment (DomCode September 2016)The road to continuous deployment (DomCode September 2016)
The road to continuous deployment (DomCode September 2016)
Michiel Rook
 
Артем Маркушев - JavaScript
Артем Маркушев - JavaScriptАртем Маркушев - JavaScript
Артем Маркушев - JavaScript
DataArt
 
Constance et qualité du code dans une équipe - Rémi Prévost
Constance et qualité du code dans une équipe - Rémi PrévostConstance et qualité du code dans une équipe - Rémi Prévost
Constance et qualité du code dans une équipe - Rémi Prévost
Web à Québec
 
Puppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with PuppetPuppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with Puppet
Puppet
 
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Sergii Khomenko
 
Introduction to pig & pig latin
Introduction to pig & pig latinIntroduction to pig & pig latin
Introduction to pig & pig latin
knowbigdata
 
Modern day jvm controversies
Modern day jvm controversiesModern day jvm controversies
Modern day jvm controversies
VictorSzoltysek
 
Developer-friendly taskqueues: What you should ask yourself before choosing one
Developer-friendly taskqueues: What you should ask yourself before choosing oneDeveloper-friendly taskqueues: What you should ask yourself before choosing one
Developer-friendly taskqueues: What you should ask yourself before choosing one
Sylvain Zimmer
 
Developer-friendly task queues: what we learned building MRQ, Sylvain Zimmer
Developer-friendly task queues: what we learned building MRQ, Sylvain ZimmerDeveloper-friendly task queues: what we learned building MRQ, Sylvain Zimmer
Developer-friendly task queues: what we learned building MRQ, Sylvain Zimmer
Pôle Systematic Paris-Region
 
Writing Safer Code With Swift
Writing Safer Code With SwiftWriting Safer Code With Swift
Writing Safer Code With Swift
Gage Herrmann
 
From 0 to ~100: Business Continuity with PostgreSQL
From 0 to ~100: Business Continuity with PostgreSQLFrom 0 to ~100: Business Continuity with PostgreSQL
From 0 to ~100: Business Continuity with PostgreSQL
Gabriele Bartolini
 
Spicy javascript: Create your first Chrome extension for web analytics QA
Spicy javascript: Create your first Chrome extension for web analytics QASpicy javascript: Create your first Chrome extension for web analytics QA
Spicy javascript: Create your first Chrome extension for web analytics QA
Alban Gérôme
 

Similar to 黑豹 ch4 ddd pattern pracrice (20)

Effective codereview | Dave Liddament | CODEiD
Effective codereview | Dave Liddament | CODEiDEffective codereview | Dave Liddament | CODEiD
Effective codereview | Dave Liddament | CODEiD
 
War between Tools and Design 2016
War between Tools and Design 2016War between Tools and Design 2016
War between Tools and Design 2016
 
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
To AWS with Ansible
To AWS with AnsibleTo AWS with Ansible
To AWS with Ansible
 
Kotlin初體驗
Kotlin初體驗Kotlin初體驗
Kotlin初體驗
 
Introduction to Continous Integration with WordPress
Introduction to Continous Integration with WordPressIntroduction to Continous Integration with WordPress
Introduction to Continous Integration with WordPress
 
Spark core
Spark coreSpark core
Spark core
 
The road to continuous deployment (DomCode September 2016)
The road to continuous deployment (DomCode September 2016)The road to continuous deployment (DomCode September 2016)
The road to continuous deployment (DomCode September 2016)
 
Артем Маркушев - JavaScript
Артем Маркушев - JavaScriptАртем Маркушев - JavaScript
Артем Маркушев - JavaScript
 
Constance et qualité du code dans une équipe - Rémi Prévost
Constance et qualité du code dans une équipe - Rémi PrévostConstance et qualité du code dans une équipe - Rémi Prévost
Constance et qualité du code dans une équipe - Rémi Prévost
 
Puppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with PuppetPuppet Camp London 2015 - Helping Data Teams with Puppet
Puppet Camp London 2015 - Helping Data Teams with Puppet
 
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
Helping Data Teams with Puppet / Puppet Camp London - Apr 13, 2015
 
Introduction to pig & pig latin
Introduction to pig & pig latinIntroduction to pig & pig latin
Introduction to pig & pig latin
 
Modern day jvm controversies
Modern day jvm controversiesModern day jvm controversies
Modern day jvm controversies
 
Developer-friendly taskqueues: What you should ask yourself before choosing one
Developer-friendly taskqueues: What you should ask yourself before choosing oneDeveloper-friendly taskqueues: What you should ask yourself before choosing one
Developer-friendly taskqueues: What you should ask yourself before choosing one
 
Developer-friendly task queues: what we learned building MRQ, Sylvain Zimmer
Developer-friendly task queues: what we learned building MRQ, Sylvain ZimmerDeveloper-friendly task queues: what we learned building MRQ, Sylvain Zimmer
Developer-friendly task queues: what we learned building MRQ, Sylvain Zimmer
 
Writing Safer Code With Swift
Writing Safer Code With SwiftWriting Safer Code With Swift
Writing Safer Code With Swift
 
From 0 to ~100: Business Continuity with PostgreSQL
From 0 to ~100: Business Continuity with PostgreSQLFrom 0 to ~100: Business Continuity with PostgreSQL
From 0 to ~100: Business Continuity with PostgreSQL
 
Spicy javascript: Create your first Chrome extension for web analytics QA
Spicy javascript: Create your first Chrome extension for web analytics QASpicy javascript: Create your first Chrome extension for web analytics QA
Spicy javascript: Create your first Chrome extension for web analytics QA
 

More from Fong Liou

拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
Fong Liou
 
黑豹 ch4 ddd pattern practice (2)
黑豹 ch4 ddd pattern practice (2)黑豹 ch4 ddd pattern practice (2)
黑豹 ch4 ddd pattern practice (2)
Fong Liou
 
2021 DDDTW Study Group 第一場 練習題
2021 DDDTW Study Group 第一場 練習題2021 DDDTW Study Group 第一場 練習題
2021 DDDTW Study Group 第一場 練習題
Fong Liou
 
Lerna 的套件管理術 - 2020 JSDC Taiwan
Lerna 的套件管理術 - 2020 JSDC TaiwanLerna 的套件管理術 - 2020 JSDC Taiwan
Lerna 的套件管理術 - 2020 JSDC Taiwan
Fong Liou
 
Example mapping
Example mappingExample mapping
Example mapping
Fong Liou
 
Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)
Fong Liou
 
Error Handling In JS
Error Handling In JSError Handling In JS
Error Handling In JS
Fong Liou
 
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
Fong Liou
 

More from Fong Liou (8)

拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
拒絕再寫無效規格,來學學實例化需求! (Agile Summit TW 2023)
 
黑豹 ch4 ddd pattern practice (2)
黑豹 ch4 ddd pattern practice (2)黑豹 ch4 ddd pattern practice (2)
黑豹 ch4 ddd pattern practice (2)
 
2021 DDDTW Study Group 第一場 練習題
2021 DDDTW Study Group 第一場 練習題2021 DDDTW Study Group 第一場 練習題
2021 DDDTW Study Group 第一場 練習題
 
Lerna 的套件管理術 - 2020 JSDC Taiwan
Lerna 的套件管理術 - 2020 JSDC TaiwanLerna 的套件管理術 - 2020 JSDC Taiwan
Lerna 的套件管理術 - 2020 JSDC Taiwan
 
Example mapping
Example mappingExample mapping
Example mapping
 
Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)Legacy code 讀書會 1st (Ch1 - Ch5)
Legacy code 讀書會 1st (Ch1 - Ch5)
 
Error Handling In JS
Error Handling In JSError Handling In JS
Error Handling In JS
 
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
2019-03-13-ddd taiwan-community-iddd-studygroup-2nd
 

Recently uploaded

Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
ijseajournal
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
sydezfe
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
Literature review for prompt engineering of ChatGPT.pptx
Literature review for prompt engineering of ChatGPT.pptxLiterature review for prompt engineering of ChatGPT.pptx
Literature review for prompt engineering of ChatGPT.pptx
LokerXu2
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
Kamal Acharya
 
Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...
pvpriya2
 
Sachpazis_Consolidation Settlement Calculation Program-The Python Code and th...
Sachpazis_Consolidation Settlement Calculation Program-The Python Code and th...Sachpazis_Consolidation Settlement Calculation Program-The Python Code and th...
Sachpazis_Consolidation Settlement Calculation Program-The Python Code and th...
Dr.Costas Sachpazis
 
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
IJCNCJournal
 
一比一原版(psu学位证书)美国匹兹堡州立大学毕业证如何办理
一比一原版(psu学位证书)美国匹兹堡州立大学毕业证如何办理一比一原版(psu学位证书)美国匹兹堡州立大学毕业证如何办理
一比一原版(psu学位证书)美国匹兹堡州立大学毕业证如何办理
nonods
 
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls ChennaiCall Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
paraasingh12 #V08
 
comptia-security-sy0-701-exam-objectives-(5-0).pdf
comptia-security-sy0-701-exam-objectives-(5-0).pdfcomptia-security-sy0-701-exam-objectives-(5-0).pdf
comptia-security-sy0-701-exam-objectives-(5-0).pdf
foxlyon
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
openshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoinopenshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoin
snaprevwdev
 
Flow Through Pipe: the analysis of fluid flow within pipes
Flow Through Pipe:  the analysis of fluid flow within pipesFlow Through Pipe:  the analysis of fluid flow within pipes
Flow Through Pipe: the analysis of fluid flow within pipes
Indrajeet sahu
 
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
DharmaBanothu
 
Advancements in Automobile Engineering for Sustainable Development.pdf
Advancements in Automobile Engineering for Sustainable Development.pdfAdvancements in Automobile Engineering for Sustainable Development.pdf
Advancements in Automobile Engineering for Sustainable Development.pdf
JaveedKhan59
 
FULL STACK PROGRAMMING - Both Front End and Back End
FULL STACK PROGRAMMING - Both Front End and Back EndFULL STACK PROGRAMMING - Both Front End and Back End
FULL STACK PROGRAMMING - Both Front End and Back End
PreethaV16
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
Northrop Grumman - Aerospace Structures Overvi.pdf
Northrop Grumman - Aerospace Structures Overvi.pdfNorthrop Grumman - Aerospace Structures Overvi.pdf
Northrop Grumman - Aerospace Structures Overvi.pdf
takipo7507
 

Recently uploaded (20)

Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
Literature review for prompt engineering of ChatGPT.pptx
Literature review for prompt engineering of ChatGPT.pptxLiterature review for prompt engineering of ChatGPT.pptx
Literature review for prompt engineering of ChatGPT.pptx
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
 
Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...
 
Sachpazis_Consolidation Settlement Calculation Program-The Python Code and th...
Sachpazis_Consolidation Settlement Calculation Program-The Python Code and th...Sachpazis_Consolidation Settlement Calculation Program-The Python Code and th...
Sachpazis_Consolidation Settlement Calculation Program-The Python Code and th...
 
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
Particle Swarm Optimization–Long Short-Term Memory based Channel Estimation w...
 
一比一原版(psu学位证书)美国匹兹堡州立大学毕业证如何办理
一比一原版(psu学位证书)美国匹兹堡州立大学毕业证如何办理一比一原版(psu学位证书)美国匹兹堡州立大学毕业证如何办理
一比一原版(psu学位证书)美国匹兹堡州立大学毕业证如何办理
 
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls ChennaiCall Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
 
comptia-security-sy0-701-exam-objectives-(5-0).pdf
comptia-security-sy0-701-exam-objectives-(5-0).pdfcomptia-security-sy0-701-exam-objectives-(5-0).pdf
comptia-security-sy0-701-exam-objectives-(5-0).pdf
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
openshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoinopenshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoin
 
Flow Through Pipe: the analysis of fluid flow within pipes
Flow Through Pipe:  the analysis of fluid flow within pipesFlow Through Pipe:  the analysis of fluid flow within pipes
Flow Through Pipe: the analysis of fluid flow within pipes
 
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
 
Advancements in Automobile Engineering for Sustainable Development.pdf
Advancements in Automobile Engineering for Sustainable Development.pdfAdvancements in Automobile Engineering for Sustainable Development.pdf
Advancements in Automobile Engineering for Sustainable Development.pdf
 
FULL STACK PROGRAMMING - Both Front End and Back End
FULL STACK PROGRAMMING - Both Front End and Back EndFULL STACK PROGRAMMING - Both Front End and Back End
FULL STACK PROGRAMMING - Both Front End and Back End
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
Northrop Grumman - Aerospace Structures Overvi.pdf
Northrop Grumman - Aerospace Structures Overvi.pdfNorthrop Grumman - Aerospace Structures Overvi.pdf
Northrop Grumman - Aerospace Structures Overvi.pdf
 

黑豹 ch4 ddd pattern pracrice

  • 1. JUST POST IT! CH4 - PRACTICE
  • 2. TEXT BEFORE WE START, YOU CAN CHECK THESE REFERENCES ▸ Java ▸ https://github.com/humank/EventStormingWorkShop ▸ https://github.com/VaughnVernon/IDDD_Samples ▸ https://github.com/ddd-by-examples/library ▸ C# ▸ https://github.com/ArthurChang01/DDDTW.CoffeeShop ▸ https://github.com/VaughnVernon/IDDD_Samples_NET ▸ https://www.wiley.com/go/domaindrivendesign Ch14, Ch15 ▸ TypeScript ▸ https://github.com/stemmlerjs/ddd-forum ▸ Php ▸ https://github.com/justericgg/dddtw-laravel-coffee-shop
  • 3. TEXT ABOUT ME ▸ Fong ▸ Backend Engineer at Carousell ▸ DDDTW Speaker, Volunteer, Organizer ▸ IT 30-day blogs contest ▸ Think in GraphQL ▸ Think in DDD
  • 4. OVERVIEW ▸ Value Object Practice ▸ Entity Practice ▸ Aggregate Root Practice ▸ Application Service ▸ Repository
  • 6. VALUE OBJECT VALUE OBJECT KEY PROPERTIES ▸ Immutability ▸ Java: fi nal or @Value ▸ Java16: Record ▸ C#: readonly ▸ JS/TS: Object.freeze ▸ Attributed-Based Equality ▸ Java: @EqualsAndHashCode ▸ C#: ▸ JS/TS ▸ Replacibility (可替換性): always return new instances in state-changing methods
  • 7. VALUE OBJECT HOW TO CREATE VALUE OBJECTS IN VALID STATE (SELF-VALIDATING) ▸ Validate in the constructor ▸ Validate (or partial validate) in Factory method (useful when dealing with different situations) ▸ Use code contracts to improve readability (tradeoff tech complexity)
  • 8. VALUE OBJECT VALID VALUE OBJECT CREATION - 1 ▸ Validate in the constructor (before/after assignation)
  • 9. VALUE OBJECT VALID VALUE OBJECT CREATION - 2 ▸ Validate (or partial validate) in Factory method (useful when dealing with different situations)
  • 10. VALUE OBJECT VALID VALUE OBJECT CREATION - 3 ▸ Use code contracts to improve readability (tradeoff tech complexity)
  • 11. GET YOUR HANDS DIRTY👨💻
  • 12. VALUE OBJECT CODE YOUR FIRST VALUE OBJECT - MONEY OBJECT! ▸ Properties ▸ Currency (enum TWD, USD, JPY), Amount ▸ Invariants: ▸ 0 < Amount < INT_MAX ▸ Currency is not null ▸ Methods: ▸ Add(Money m): Money ▸ Subtract(Money m) Money ▸ Factories ▸ Static ofUSD(decimal amount): Money ▸ Static ofTWD(decimal amount): Money https://github.com/tdd-best/tdd-by-example-the-money/blob/master/src/main/java/money/Money.java
  • 13. VALUE OBJECT CODE YOUR FIRST VALUE OBJECT - MONEY OBJECT! PART 1 ▸ Properties ▸ Currency (enum TWD, USD, JPY) ▸ Amount ▸ Invariants: ▸ 0 < Amount < 1M ▸ Currency is not null ▸ Test Cases ▸ 10 TWD = 10 TWD ▸ 10 TWD != 20 TWD ▸ 10 TWD != 10 USD ▸ Hints ▸ Properties are immutable (java 16: record, java: fi nal, @value, c#: readonly, ts: readonly) ▸ Override equals function
  • 14. VALUE OBJECT CODE YOUR FIRST VALUE OBJECT - MONEY OBJECT! PART 2 ▸ Methods: ▸ Add (Money augend) -> Money ▸ Cannot add money in different currency ▸ The result cannot be over 1 M ▸ Subtract (Money subtrahend) -> Money ▸ Cannot subtract money in different currency ▸ The result cannot be lower and equal to 0 ▸ Test Cases ▸ 10 TWD + 10 TWD = 20 TWD ▸ 10 TWD + 20 USD -> Exception! ▸ 10 TWD - 5 TWD = 5 TWD ▸ 10 TWD - 1 USD -> Exception! ▸ 10 TWD - 20 TWD -> Exception! https://github.com/tdd-best/tdd-by-example-the-money/blob/master/src/main/java/money/Money.java
  • 15. VALUE OBJECT CODE YOUR FIRST VALUE OBJECT - MONEY OBJECT! PART 3 ▸ Factories ▸ Static ofUSD(decimal amount): Money ▸ Static ofTWD(decimal amount): Money ▸ Static ofJYP(decimal amount): Money https://github.com/tdd-best/tdd-by-example-the-money/blob/master/src/main/java/money/Money.java
  • 17. VALUE OBJECT POSTDATA ▸ Location: domain/model/PostData ▸ PostData ▸ Properties ▸ Subject ▸ Subject cannot be empty ▸ Body ▸ Body should below 100k words ▸ Methods: ▸ Change(Subject, Body): PostData https://github.com/tdd-best/tdd-by-example-the-money/blob/master/src/main/java/money/Money.java
  • 19. ENTITY ENTITY KEY PROPERTIES ▸ Must have an ID that can be tracked down the path, and the ID is unique and cannot be changed ▸ Usually has states ▸ Methods with rich behaviors ▸ Public getter/private setter
  • 20. ENTITY PROBLEMS WHEN YOU CREATE AN ENTITY ▸ How to make ID more explicit? ▸ When to generate the Entity ID? ▸ How to make sure
  • 21. GET YOUR HANDS DIRTY👨💻
  • 22. ENTITY POST ▸ Location: domain/model/Post ▸ Properties ▸ ForumId: class ForumId (id: int) ▸ ID: class PostID (id: int) ▸ PostData ▸ creationDate ▸ modi fi edDate ▸ Public getter/private setter
  • 23. ENTITY POST FACTORY ▸ Static publishPost(forumId, postId, postData) -> Post
  • 24.
  • 25. THEN, WHO’S THE AGGREGATE ROOT?
  • 26. AGGREGATE ROOT & REPOSITORY PRACTICE ENTRANCE FOR ALL
  • 27. AGGREGATE ROOT AGGREGATE ROOT KEY PROPERTIES ▸ Must have an Entity acting as an aggregate root ▸ You can only use aggregate root to update the objects inside the aggregate root
  • 28. GET YOUR HANDS DIRTY👨💻
  • 29. AGGREGATE & REPOSITORY POST AS AGGREGATE ROOT ▸ Requirements: ▸ updateContent(PostData): void
  • 30. APPLICATION SERVICE THE CONDUCTOR Aggregate Root Repository External Service
  • 31. GET YOUR HANDS DIRTY👨💻
  • 32. APPLICATION SERVICE PUBLISH NEW POST ▸ Location: application/PostApplicationSvc ▸ Class PostApplicastionSvc ▸ publishNewPost(String subject, String body, int forumId) ▸ Post post = Post.publishPost(… … …. ); ▸ You can just assign a random number to id for now
  • 33. APPLICATION SERVICE WHAT DO WE NEED NEXT? ▸ ID generator to encapsulate the implementation detail ▸ Persistent the aggregate root
  • 35. REPOSITORY REPOSITORY KEY PROPERTIES ▸ Relate to aggregate root, one aggregate root per repository ▸ Only create new methods when you need it in business!!!! ▸ Repository Interface in domain layer, Repository Implements in infra layer
  • 36. GET YOUR HANDS DIRTY👨💻
  • 37. REPOSITORY POST REPOSITORY ▸ Location: domain/model/PostRepository ▸ Interface PostRepository ▸ nextId() -> PostId ▸ save(Post) -> void ▸ Location: infrastructure/persistence/InMemoryPostRepository ▸ Class InMemoryPostRepository implements PostRepository ▸ HashMap<PostId, Post> maps ▸ nextId() -> PostId ▸ save(Post) -> void
  • 38. REPOSITORY BACK TO APPLICATION SERVICES ▸ Id = postRepo.nextId(); ▸ Post = Post.publishPost(id) ▸ postRepo.save(post)
  • 40. TEXT REFERENCES ▸ Implementing Domain-Driven Design ▸ Practice, Pattern, Principle of Domain-Driven Design ▸ https://github.com/tdd-best/tdd-by-example-the-money/tree/master/src/main/ java/money ▸ https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-writing- nested-tests/ ▸ https://softwareengineering.stackexchange.com/questions/367145/is-the- builder-pattern-applicable-in-domain-driven-design