ddd-rail your monorail
TRANSCRIPT
![Page 1: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/1.jpg)
DDD-rail Your MonorailBreaking up the Rails monolith
with domain driven design
🚝💥🚀
![Page 2: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/2.jpg)
Hi, I’m Andrew
![Page 3: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/3.jpg)
![Page 4: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/4.jpg)
Meet Delorean “It’s like Uber, for time travel!”
🚗🚀
![Page 5: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/5.jpg)
The situation
![Page 6: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/6.jpg)
%💨😢😱😡
![Page 7: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/7.jpg)
What happened?
![Page 8: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/8.jpg)
In the beginning, there was an app.
![Page 9: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/9.jpg)
The business decides…• Drivers deliver passengers from year A to
year B. 🚕⌛
![Page 10: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/10.jpg)
![Page 11: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/11.jpg)
![Page 12: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/12.jpg)
💰😂
![Page 13: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/13.jpg)
The business decides…• Drivers deliver passengers from year A to
year B. 🚕⌛ • Passengers choose the type of Delorean
service they want. 🚕 🚗 🚐
![Page 14: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/14.jpg)
![Page 15: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/15.jpg)
![Page 16: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/16.jpg)
💰💰💰😂📰
![Page 17: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/17.jpg)
The business decides…• Drivers deliver passengers from year A to
year B. 🚕⌛ • Passengers choose the type of Delorean
service they want. 🚕 🚗 🚐 • Time travel carpools! 🚕01
![Page 18: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/18.jpg)
![Page 19: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/19.jpg)
![Page 20: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/20.jpg)
💰💰💰💰💰😂📰
![Page 21: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/21.jpg)
The business decides…• Drivers deliver passengers from year A to year B. 🚕⌛ • Passengers choose the type of Delorean service they
want. 🚕 🚗 🚐 • Time travel carpools! 🚕01 • DeloreanEATS: Customers order food, drivers pick
up from time period and deliver to customer time period! 🚕🍔
![Page 22: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/22.jpg)
![Page 23: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/23.jpg)
![Page 24: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/24.jpg)
💩😐📰
![Page 25: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/25.jpg)
Hm.
![Page 26: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/26.jpg)
![Page 27: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/27.jpg)
💣 Regressions
![Page 28: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/28.jpg)
The Payments team regresses Trip while refactoring ServiceTier
![Page 29: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/29.jpg)
The Restaurants team deploys a new pricing algorithm that regresses rideshare pricing
![Page 30: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/30.jpg)
💣 Responsibilities?
![Page 31: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/31.jpg)
The Mobile team requests a new mobile login API, but which team should implement it?
![Page 32: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/32.jpg)
💣 Business won’t stop
![Page 33: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/33.jpg)
Outsourced dev shop is rebuilding the marketing home page and needs a pricing API.
![Page 34: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/34.jpg)
CEO wants to launch "Airbnb for time travel” feature in, let’s say, 2 months!
![Page 35: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/35.jpg)
Does this sound familiar?
![Page 36: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/36.jpg)
There are deeper insights to be had
![Page 37: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/37.jpg)
What is DDD?
![Page 38: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/38.jpg)
A set of techniques to arrive at a flexible design that cleanly maps
to the business model
![Page 39: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/39.jpg)
Strong, expressive domain models
![Page 40: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/40.jpg)
There is no such thing as a One True Perfect model
![Page 41: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/41.jpg)
Embrace the chaos
![Page 42: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/42.jpg)
Domain-Driven Design: Eric Evans
Implementing Domain-Driven Design: Vaughn
Vernon
![Page 43: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/43.jpg)
💥 Let’s get designing!
![Page 44: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/44.jpg)
Step 1: Visualize your domain models
![Page 46: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/46.jpg)
Ruby gem to generate UML diagrams from your ActiveRecord models
![Page 47: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/47.jpg)
![Page 48: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/48.jpg)
This helps you get the entire system into your mind.
![Page 49: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/49.jpg)
Print it out!✏📎
![Page 50: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/50.jpg)
Step 2: Find your core- and sub-domains
![Page 51: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/51.jpg)
Concept: Core Domain
![Page 52: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/52.jpg)
What a business does
![Page 53: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/53.jpg)
Transportation Core Domain
![Page 54: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/54.jpg)
Concept: Subdomains
![Page 55: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/55.jpg)
A supporting unit within the business
![Page 56: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/56.jpg)
Rideshare Subdomain Food Delivery Subdomain Financial Transaction Subdomain Identity and Access Subdomain
![Page 57: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/57.jpg)
Ridesharing
Food Delivery
Financial Transactions
Identity/Access
![Page 58: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/58.jpg)
Step 3: Get the team talking in the same language
![Page 59: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/59.jpg)
Concept: Ubiquitous Language
![Page 60: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/60.jpg)
A defined language that is used consistently across business and
technical contexts
![Page 61: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/61.jpg)
Bring in the domain experts
![Page 62: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/62.jpg)
A Driver picks up a Passenger
![Page 63: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/63.jpg)
A Passenger requests a Pickup
![Page 64: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/64.jpg)
An Owner drives a Vehicle
![Page 65: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/65.jpg)
An Operator? drives a Vehicle
![Page 66: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/66.jpg)
A User logs in and changes her Password
![Page 67: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/67.jpg)
An Invoice is delivered to the Passenger
![Page 68: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/68.jpg)
A Customer orders an item from a Menu, which is picked up and delivered by the
Delivery Agent
![Page 69: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/69.jpg)
Step 4: Make a glossary
![Page 70: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/70.jpg)
Ridesharing• Passenger: “…” • Driver: “…” • Trip: “…” • Pickup: “…” • Vehicle: “…” • Vehicle Owner: “…”
![Page 71: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/71.jpg)
Financial Transaction• Invoice: “…” • Order: “…” • Payment: “…” • Royalty: “…” • Salary: “…”
![Page 72: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/72.jpg)
Identity and Access
• User: “…” • Secure password: “…” • Role: “…”
![Page 73: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/73.jpg)
Print ‘em out!✏📎
![Page 74: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/74.jpg)
Step 5: Draw out your software systems (bounded contexts)
![Page 75: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/75.jpg)
Concept: Bounded Contexts
![Page 76: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/76.jpg)
A software system that defines the applicability of a ubiquitous
language
![Page 77: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/77.jpg)
FoodDeliveryService TripRoutingEngine
“menu” “trip”
“restaurant” “plan”
“delivery” “pickup”
“customer date” “destination”
![Page 78: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/78.jpg)
Software systems are natural boundaries for these linguistic terms
![Page 79: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/79.jpg)
If a term leaks into a different software system/bounded context, you have a smell
![Page 80: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/80.jpg)
RidesharingFinancial
TransactionsIdentity/Access
Food Delivery
DeloreanMonolith
StripeAPI
![Page 81: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/81.jpg)
DeloreanMonolith
RidesharingFinancial
TransactionsIdentity/Access
Stripe API
Food Delivery
![Page 82: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/82.jpg)
DeloreanMonolith
TransactionsIdentity/Access
Food Delivery
Driver Routing Service
Fraud Detection
Service (etc)
![Page 83: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/83.jpg)
DeloreanMonolith
RidesharingFinancial
TransactionsIdentity/Access
Stripe API
Food Delivery
![Page 84: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/84.jpg)
Step 6: Now add directional dependencies
![Page 85: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/85.jpg)
DeloreanMonolith
RidesharingFinancial
TransactionsIdentity/Access
Stripe API
Food Delivery
U
D
![Page 86: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/86.jpg)
DeloreanMonolith
TransactionsIdentity/Access
Food Delivery
Driver Routing Service
Fraud Detection
Service (etc)
U
UD
U
D U
D
![Page 87: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/87.jpg)
This helps you see dependencies between teams, where communication will be most
important.
![Page 88: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/88.jpg)
Context Map: A tool to visualize how your software systems relate to each other and
the domain(s)
![Page 89: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/89.jpg)
Our goal is to map our bounded contexts directly to our subdomains.
![Page 90: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/90.jpg)
RidesharingFinancial
TransactionsIdentity/Access
Stripe API
Food Delivery
![Page 91: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/91.jpg)
Show me the code!
![Page 92: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/92.jpg)
Tactic 1: Get the names right
![Page 93: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/93.jpg)
Adjust your class and method names to reflect the ubiquitous language
![Page 94: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/94.jpg)
# old User.without_drivers # new Passenger.hailing_drivers
# old order.calculate_cost # new order.calculate_billing_total
![Page 95: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/95.jpg)
Tactic 2: Namespace and modulize your domains
![Page 96: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/96.jpg)
Break Rails’ folder structure conventions
![Page 97: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/97.jpg)
app/domains/financial app/domains/financial/inflation_adjustment.rb app/domains/financial/invoice.rb app/domains/food_delivery app/domains/food_delivery/menu.rb app/domains/food_delivery/menu_item.rb app/domains/food_delivery/menu_items_controller.rb app/domains/food_delivery/menus_controller.rb app/domains/identity app/domains/identity/user.rb app/domains/identity/users_controller.rb app/domains/rideshare app/domains/rideshare/driver.rb app/domains/rideshare/passenger.rb app/domains/rideshare/service_tier.rb
![Page 98: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/98.jpg)
class Menu < ActiveRecord::Base belongs_to :restaurant end
![Page 99: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/99.jpg)
module FoodDelivery class Menu < ActiveRecord::Base belongs_to :restaurant end end
![Page 100: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/100.jpg)
class Trip < ActiveRecord::Base belongs_to :service_tier belongs_to :vehicle end
![Page 101: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/101.jpg)
module Rideshare class Trip < ActiveRecord::Base belongs_to :service_tier belongs_to :vehicle end end
![Page 102: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/102.jpg)
Domain code stays together
![Page 103: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/103.jpg)
Tactic 3: Design with Aggregate Roots
![Page 104: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/104.jpg)
Aggregate: A collection of domain objects that can be treated as a single unit
![Page 105: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/105.jpg)
Use aggregates to model real-world entities that belong together
![Page 106: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/106.jpg)
Aggregate Root: An entity at the “top” of the collection that can represent the whole
![Page 107: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/107.jpg)
FoodDelivery aggregate root: Order
![Page 108: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/108.jpg)
Food Delivery
![Page 109: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/109.jpg)
A user adds a menu item to their cart
![Page 110: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/110.jpg)
A User adds a MenuItem into their ShoppingCart
![Page 111: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/111.jpg)
module Financial class ShoppingCart def order @order ||= FoodDelivery::Order.new end
def amount @order.menu_items.sum(&:cost) + @order.menu_items.sum(&:tax_amount) end
def add(item) order.menu_items << item end
def remove(item) order.menu_items.delete(item); order.save end end end
![Page 112: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/112.jpg)
module FoodDelivery class Order < ActiveRecord::Base belongs_to :user has_many :order_menu_items has_many :menu_items, through: :order_menu_items end end
![Page 113: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/113.jpg)
module FoodDelivery class Order < ActiveRecord::Base belongs_to :user has_many :order_menu_items has_many :menu_items, through: :order_menu_items
def total_cost item_cost + tax_cost end
def item_cost menu_items.sum(&:cost) end
def tax_cost menu_items.sum(&:tax_amount) end
def add_item!(menu_item) menu_items << menu_item end
def remove_item!(menu_item) menu_items.delete(menu_item); save end end end
![Page 114: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/114.jpg)
module Financial class ShoppingCart def order @order ||= FoodDelivery::Order.new end
def amount @order.total_cost end
def add(item) order.add_item!(item) end
def remove(item) order.remove_item!(item) end end end
Calculation logic kept in FoodDelivery domain
Implementation-agnostic
![Page 115: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/115.jpg)
The root items of these aggregates are the only entities that external callers may fetch
![Page 116: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/116.jpg)
Ridesharing
Financial Transactions
Identity/Access
Food Delivery
![Page 117: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/117.jpg)
Building good interfaces for a service oriented future!
![Page 118: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/118.jpg)
Tactic 4: Break database joins between domains
![Page 119: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/119.jpg)
`has_many`-itis!
![Page 120: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/120.jpg)
module Rideshare class Trip < ActiveRecord::Base belongs_to :service_tier has_many :trip_pool_trips has_one :trip_pool, through: :trip_pool_trips belongs_to :driver, foreign_key: :driver_id, class_name: Identity::User belongs_to :passenger, foreign_key: :passenger_id, class_name: Identity::User belongs_to :vehicle belongs_to :order, class_name: FoodDelivery::Order has_one :payment end
![Page 121: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/121.jpg)
These tend to happen in your God Objects
![Page 122: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/122.jpg)
![Page 123: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/123.jpg)
module Rideshare class Trip < ActiveRecord::Base belongs_to :service_tier has_many :trip_pool_trips has_one :trip_pool, through: :trip_pool_trips belongs_to :driver, foreign_key: :driver_id, class_name: Identity::User belongs_to :passenger, foreign_key: :passenger_id, class_name: Identity::User belongs_to :vehicle belongs_to :order, class_name: FoodDelivery::Order has_one :payment end
![Page 124: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/124.jpg)
module Rideshare class Trip < ActiveRecord::Base # belongs_to :order, class_name: FoodDelivery::Order def order FoodDeliveryAdapter.new.order_from_trip(self) end end end
![Page 125: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/125.jpg)
module Rideshare class FoodDeliveryAdapter def order_from_trip(trip) FoodDelivery::Order.find_by(trip_id: trip.id) end end end
![Page 126: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/126.jpg)
Decoupling domains now will ease your architectural transitions later
![Page 127: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/127.jpg)
You can do this all in small steps!
![Page 128: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/128.jpg)
Toward a distributed architecture
![Page 129: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/129.jpg)
Namespaced, Isolated Modules to Rails Engines
![Page 131: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/131.jpg)
Rails engines to a remote (micro-)service
![Page 132: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/132.jpg)
Good architecture goes a very long way.
![Page 133: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/133.jpg)
To recap🚕 💨
![Page 134: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/134.jpg)
Drew things on a wall
![Page 135: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/135.jpg)
Came up with a language
![Page 136: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/136.jpg)
Moved code around
![Page 137: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/137.jpg)
Team is on the same page
![Page 138: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/138.jpg)
Big idea: Your software speaks the same language as your domain
experts
![Page 139: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/139.jpg)
Big idea: Bounded contexts are separators for linguistic drivers. Keep your language
consistent in one and only one system
![Page 140: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/140.jpg)
Big idea: Domain code should live together
![Page 141: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/141.jpg)
Big idea: Adapters between domains enforce domain purity
![Page 142: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/142.jpg)
Big idea: Incremental changes
![Page 143: DDD-rail Your Monorail](https://reader033.vdocuments.site/reader033/viewer/2022051101/588913071a28ab4a5c8b5f49/html5/thumbnails/143.jpg)
Thanks!Sample code: https://github.com/andrewhao/delorean Slides: https://github.com/andrewhao/dddrail-talk
Twitter @andrewhaoGithub @andrewhao
8