-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathddd-toolkit-2.pug
More file actions
406 lines (384 loc) · 15.3 KB
/
ddd-toolkit-2.pug
File metadata and controls
406 lines (384 loc) · 15.3 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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(data-background-image=require("./pics/startup-process.png").default data-background-size="contain" data-background-color="#2E677B")
section(data-background-image=require("./pics/long-function.jpg").default data-background-size="contain")
section
h2 pros
ol
li.fragment Relatively easy to read
h2.fragment cons
ol
li.fragment Really hard to change
li.fragment We can not see the whole picture
section(data-background-image=require("./pics/trello-issue.png").default data-background-size="contain" data-background-color="#002B45")
section
h2 Implicit API
pre
code.python.
class Purchases(viewsets.ModelViewSet):
queryset = Purchase.objects.all()
serializer_class = PurchaseSerializer
permission_classes = (CanPurchase,)
filter_class = PurchaseFilter
pre
code.python.
router.register('/api/purchases/', Purchases)
ol
li.fragment What exactly does this class do?
li.fragment How to use it?
section
h2 pros
ol
li.fragment Relatively easy to change
h2.fragment cons
ol
li.fragment Really hard to read
li.fragment You should keep in mind framework rules
li.fragment Implicit knowledge grow
li.fragment We still can not see the whole picture
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
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(data-background-image=require("./pics/eric-evans.jpg").default data-background-size="contain" data-background-position="left" data-background-color="#000000")
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(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 Purchase:
@story
@arguments("invoice_id", "user")
def make(I):
I.find_order
I.find_price
I.find_invoice
I.check_balance
I.persist_payment
I.persist_purchase
I.send_purchase_notification
section
h2 Steps implementation
pre
code.python.
from stories import Failure, Success
class Purchase:
# ...
def find_invoice(self, ctx):
invoice = Invoice.objects.get(pk=ctx.invoice_id)
return Success(invoice=invoice)
def check_balance(self, ctx):
if ctx.user.can_pay(ctx.invoice):
return Success()
else:
return Failure()
section
h2 Story Execution
pre
code.python.
>>> Purchase().make(category_id=2)
Purchase.make:
find_order
find_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 Purchase.find_category
section
h2 pros
ol
li.fragment Clean flow in the source code
li.fragment Separate step implementation
li.fragment Each step knows nothing about a neighbor
li.fragment Easy reuse of code
li.fragment Allows to instrument code easily
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
img(src=require("./pics/postgresql.png").default height="150" style=" padding-right: 70px;").plain
img(src=require("./pics/firebase.png").default height="150" style=" padding-right: 70px;").plain
ul
li.fragment We do not have the tooling to work with data
li.fragment There are no data contracts written in code
section
img(src=require("./pics/attrs.png").default).plain
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
h2 State Contract
pre
code.python.
from pydantic import BaseModel
class Purchase:
@story
def make(I):
# ...
@Purchase.make.contract
class Context(BaseModel):
user: User
invoice_id: InvoiceId
invoice: Optional[Invoice]
section
h2 pros
ol
li.fragment Explicit data contracts and relations in code
li.fragment Data store independent
li.fragment Catch errors when they occur
li.fragment Not when they propagate to exception
h2.fragment cons
ol
li.fragment Working with data sources manually
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": "id"})
@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.aggregates 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
img(src=require("./pics/pusher.png").default).plain
section
h2 How we use third-party libraries
pre
code.python.
from pusher import Pusher
class Purchase:
def send_purchase_notification(ctx):
Pusher().trigger("private-user-1")
pre
code.python.
def test_before(monkeypatch):
monkeypatch.setattr(pusher, "Pusher", Mock())
# ...
pusher.Pusher.trigger.assert_called_once_with(
"private-user-1"
)
section
img(src=require("./pics/ably.png").default).plain
section
h2 How to use it with DI
pre
code.python.
@dataclass
class Purchase:
def send_purchase_notification(ctx):
self.trigger_message(UserStream(ctx.user))
trigger_message: Emitter
pre
code.python.
def test_after(emitter):
# ...
Purchase.trigger_message.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 Layout
pre
code.
project
├── admin
├── aggregates
├── forms
├── migrations
├── models
├── repositories
├── services
├── templates
├── #[b implementation]
└── views
section
h2 Injection
pre
code.python.
from dependencies import Injector, Package
app = Package('app')
class MakePurchase(Injector):
make_purchase = app.services.Purchase.make
load_category = app.repositories.load_category
load_price = app.repositories.load_price
load_profile = app.repositories.load_profile
MakePurchase.make_purchase(category_id=1, price_id=1)
section(data-background-image=require("./pics/keep-calm-and-ddd.png").default data-background-size="contain" data-background-color="#C10C06")
section
h2 Get in touch
ul
li
a(href="https://dry-python.org/") dry-python.org
li
a(href="https://twitter.com/dry_py") twitter.com/dry_py
li
a(href="https://github.com/dry-python") github.com/dry-python
li
a(href="https://gitter.im/dry-python") gitter.im/dry-python