-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathddd-toolkit.pug
More file actions
318 lines (297 loc) · 12.5 KB
/
ddd-toolkit.pug
File metadata and controls
318 lines (297 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
doctype html
html
head
meta(charset="utf-8")
meta(name="description" content="Domain Driven Design Toolkit")
meta(name="author" content="Artem Malyshev")
meta(name="apple-mobile-web-app-capable" content="yes")
meta(name="apple-mobile-web-app-status-bar-style" content="black-translucent")
meta(name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no")
title Domain Driven Design Toolkit
body
.reveal
.slides
section
h2 Domain Driven Design Toolkit
h4
a(href="https://github.com/proofit404") Artem Malyshev
h6
a(href="https://twitter.com/proofit404") @proofit404
section
h2 BIO
ul
li Co-Founder at #[a(href="https://drylabs.io/") drylabs.io]
li #[a(href="https://dry-python.org/") dry-python.org]
li Django Channels 1.0
li 5 years of experience in Python
section
h2 Complexity
p.fragment #[b Accidental complexity] refers to challenges that developers unintentionally make for themselves as a result of trying to solve a problem.
p.fragment #[b Essential complexity] is just the nature of the beast you're trying to tame.
section(data-background-image=require("./pics/interrupted-programmer.jpg").default data-background-size="contain")
section(data-background-image=require("./pics/callback-hell.jpg").default data-background-size="contain")
section
h2 Accidental complexity
ul
li.fragment AsyncIO vs. Gevent
li.fragment PostgreSQL vs. MongoDB
li.fragment Python vs. Go
li.fragment Emacs vs. Vim
li.fragment Tabs vs. Spaces
section
img(src=require("./pics/domain-driven-design.jpg").default).plain
section
h2 What is the domain-driven design?
p.fragment Focus on the core complexity and opportunity in the domain
p.fragment Explore models in a collaboration of domain experts and software experts
p.fragment Write software that expresses those models explicitly
p.fragment Speak #[b ubiquitous language] within a #[b bounded context]
section
h2 Not about technology
p.fragment You have to master the tool first then you can focus on #[b DDD].
p.fragment In most domain models, most design patterns are technical noise.
section
img(src=require("./pics/implementing-domain-driven-design.jpg").default).plain
section(data-background-image=require("./pics/ddd-schema.jpg").default data-background-size="contain")
section(data-background-image=require("./pics/ddd-schema-models-selected.jpg").default data-background-size="contain")
section
h2 What is a model?
p.fragment #[b HINT:] Not a UML diagram
p.fragment A set of services, entities and value objects expressed in classes, methods, and refs.
section
h2
a(href="https://dry-python.org/") dry-python
p A set of libraries for pluggable business logic components.
img(src="https://raw.githubusercontent.com/dry-python/brand/master/logo/dry-python.png").plain
section(data-background-image=require("./pics/ddd-schema-services-selected.jpg").default data-background-size="contain")
section
img(src="https://raw.githubusercontent.com/dry-python/brand/master/logo/stories.png").plain
p Define a user story in the business transaction DSL.
p Separate state, implementation and specification.
section
h2 Layout
pre
code.
project
├── admin
├── forms
├── migrations
├── models
├── #[b services]
├── templates
└── views
section
h2 Specification DSL
pre
code.python.
from stories import story, arguments
class Subscription:
@story
@arguments("invoice_id", "user")
def buy(I):
I.find_order
I.find_price
I.find_invoice
I.check_balance
I.persist_payment
I.persist_subscription
I.send_subscription_notification
section
h2 State Contract
pre
code.python.
from pydantic import BaseModel
class Subscription:
@story
def buy(I):
...
@Subscription.buy.contract
class Context(BaseModel):
user: User
invoice_id: int
invoice: Optional[Invoice]
section
h2 Steps implementation
pre
code.python.
from stories import Failure, Success
class Subscription:
def find_invoice(self, ctx: Context):
invoice = Invoice.objects.get(pk=ctx.invoice_id)
return Success(invoice=invoice)
def check_balance(self, ctx: Context):
if ctx.user.can_pay(ctx.invoice):
return Success()
else:
return Failure()
section
h2 Story Execution
pre
code.python.
>>> Subscription().buy(category_id=2)
Subscription.buy:
find_category
check_price
check_purchase (PromoCode.validate)
find_code (skipped)
check_balance
find_profile
Context:
category_id = 1318 # Story argument
user = #{"<"}User: 3292#{">"} # Story argument
category = #{"<"}Category: 1318#{">"}
# Set by Subscription.find_category
section(data-background-image=require("./pics/debug-toolbar.png").default data-background-size="contain")
h2 DEBUG TOOLBAR
br
br
br
br
br
br
br
br
br
br
section(data-background-image=require("./pics/pytest.png").default data-background-size="contain")
h2(style="color: white") py.test
section(data-background-image=require("./pics/sentry.png").default data-background-size="contain")
h2 Sentry
section(data-background-image=require("./pics/elk-logs-ui.gif").default data-background-size="contain")
h2 ELK
section(data-background-image=require("./pics/ddd-schema-entities-selected.jpg").default data-background-size="contain")
section(data-background-image=require("./pics/aggregate-root-1.jpg").default data-background-size="contain")
section(data-background-image=require("./pics/aggregate-root-2.jpg").default data-background-size="contain")
section(data-background-image=require("./pics/aggregate-root-3.jpg").default data-background-size="contain")
section(data-background-image=require("./pics/aggregate-root-4.jpg").default data-background-size="contain")
section(data-background-image=require("./pics/aggregate-root-5.jpg").default data-background-size="contain")
section
h2 Layout
pre
code.
project
├── admin
├── #[b aggregates]
├── forms
├── migrations
├── models
├── services
├── templates
└── views
section
h2 dataclasses
pre
code.python.
from dataclasses import dataclass
from typing import List, NewType
OrderId = NewType("OrderId", int)
@dataclass
class LineItem:
product_id: ProductId
@dataclass
class Order:
primary_key: OrderId
items: List[LineItem]
section(data-background-image=require("./pics/ddd-schema-repositories-selected.jpg").default data-background-size="contain")
section
img(src="https://raw.githubusercontent.com/dry-python/brand/master/logo/mappers.png").plain
p Declarative mappers from ORM models to domain entities. And back again!
section
h2 Layout
pre
code.
project
├── admin
├── aggregates
├── forms
├── migrations
├── models
├── #[b repositories]
├── services
├── templates
└── views
section
h2 Django ORM
pre
code.python.
from mappers import Mapper
from app.aggregates import Order, OrderId, User
from app.models import OrderModel, UserModel
mapper = Mapper(Order, OrderModel, {"primary_key": "pk"})
@mapper.reader
def load_order(id: OrderId, user: User) -> Order:
friends = UserModel.objects.filter(
purchases=OuterRef("pk"), friends=user.primary_key)
return OrderModel.objects.filter(pk=id).annotate(
purchased_by_friends=Exists(friends)).get()
section
h2 Swagger definitions
pre
code.python.
from mappers import Mapper
from bravado import swagger_model
from app.aggregates import Price
spec = swagger_model.load_file("price_service.yml")
mapper = Mapper(Price, spec.definitions["Price"])
@mapper.reader
def load_price(id: PriceId) -> Price:
return requests.get(f"http://172.16.1.7/get/{id}")
section
h2 GraphQL queries
pre
code.python.
from mappers import Mapper
from gql import gql, Client, build_schema
from app.models import Invoice
schema = build_schema("invoice_service.graphql")
mapper = Mapper(Invoice, schema.get_type_map()["Invoice"])
@mapper.reader
def load_invoice(id: InvoiceId) -> Invoice:
return Client(schema=schema).execute(gql("""
{
loadInvoice(id: %(id)d)
}
""", {"id": id}))
section(data-background-image=require("./pics/pusher-to-ably.png").default data-background-size="contain")
section
h2 Tests & Mocks
pre
code.python.
def test_before(monkeypatch):
monkeypatch.setattr(pusher, "Pusher", Mock())
# ...
pusher.Pusher.trigger.assert_called_once_with(
"private-user-1"
)
def test_after(emitter):
# ...
emitter.trigger.assert_called_once_with(
UserStream(User(primary_key=1))
)
section
img(src="https://raw.githubusercontent.com/dry-python/brand/master/logo/dependencies.png").plain
p Provide composition instead of inheritance.
p Solves top-down approach problem.
section
h2 Dependency Injection
pre
code.python.
from dependencies import Injector, Package
app = Package("project")
class BuySubscription(Injector):
buy_subscription = app.services.Subscription.buy
load_order = app.repositories.load_order
load_price = app.repositories.load_price
load_invoice = app.repositories.load_invoice
BuySubscription.buy_subscription(category_id=1, price_id=1)
section
h2 Refactoring roadmap
ul
li.fragment No DDD at all
li.fragment Stories without contracts
li.fragment Contracts and aggregates
li.fragment Mappers
li.fragment Dependency injection
section(data-background-image=require("./pics/drylabs-io.png").default data-background-size="contain")
section
h2 Questions?