-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathddd-toolkit-2.html
More file actions
162 lines (140 loc) · 12.8 KB
/
Copy pathddd-toolkit-2.html
File metadata and controls
162 lines (140 loc) · 12.8 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
<!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,maximum-scale=1,user-scalable=no"><title>Domain Driven Design Toolkit</title><link href="reveal.css" rel="stylesheet"></head><body><div class="reveal"><div class="slides"><section><h2>Domain Driven Design Toolkit</h2><h4><a href="https://github.com/proofit404">Artem Malyshev</a></h4><h6><a href="https://twitter.com/proofit404">@proofit404</a></h6></section><section><h2>BIO</h2><ul><li>Co-Founder at <a href="https://drylabs.io/">drylabs.io</a></li><li><a href="https://dry-python.org/">dry-python.org</a></li><li>Django Channels 1.0</li><li>5 years of experience in Python</li></ul></section><section data-background-image="48ac918b7c24e57b443ca4ae9d10aeb9.png" data-background-size="contain" data-background-color="#2E677B"></section><section data-background-image="8ef1b2a6453aba2b739fcec3b7d92cfc.jpg" data-background-size="contain"></section><section><h2>pros</h2><ol><li class="fragment">Relatively easy to read</li></ol><h2 class="fragment">cons</h2><ol><li class="fragment">Really hard to change</li><li class="fragment">We can not see the whole picture</li></ol></section><section data-background-image="0344ddd95d4b944a5a9874cee6d7198f.png" data-background-size="contain" data-background-color="#002B45"></section><section><h2>Implicit API</h2><pre><code class="python">class Purchases(viewsets.ModelViewSet):
queryset = Purchase.objects.all()
serializer_class = PurchaseSerializer
permission_classes = (CanPurchase,)
filter_class = PurchaseFilter</code></pre><pre><code class="python">router.register('/api/purchases/', Purchases)</code></pre><ol><li class="fragment">What exactly does this class do?</li><li class="fragment">How to use it?</li></ol></section><section><h2>pros</h2><ol><li class="fragment">Relatively easy to change</li></ol><h2 class="fragment">cons</h2><ol><li class="fragment">Really hard to read</li><li class="fragment">You should keep in mind framework rules</li><li class="fragment">Implicit knowledge grow</li><li class="fragment">We still can not see the whole picture</li></ol></section><section><h2>Complexity</h2><p class="fragment"><b>Accidental complexity</b> refers to challenges that developers unintentionally make for themselves as a result of trying to solve a problem.</p><p class="fragment"><b>Essential complexity</b> is just the nature of the beast you're trying to tame.</p></section><section><h2>Accidental complexity</h2><ul><li class="fragment">AsyncIO vs. Gevent</li><li class="fragment">PostgreSQL vs. MongoDB</li><li class="fragment">Python vs. Go</li><li class="fragment">Emacs vs. Vim</li><li class="fragment">Tabs vs. Spaces</li></ul></section><section data-background-image="de3f947fd066cd3377df0eb5ccbbf393.jpg" data-background-size="contain" data-background-position="left" data-background-color="#000000"></section><section><img class="plain" src="0f97676130bd6321766a03b91159f7b1.jpg"></section><section><h2>What is the domain-driven design?</h2><p class="fragment">Focus on the core complexity and opportunity in the domain</p><p class="fragment">Explore models in a collaboration of domain experts and software experts</p><p class="fragment">Write software that expresses those models explicitly</p><p class="fragment">Speak <b>ubiquitous language</b> within a <b>bounded context</b></p></section><section data-background-image="7342db757b86c2aceb1a6b33d4ddce96.jpg" data-background-size="contain"></section><section data-background-image="07fb3115c562bce7922a441f21b2754f.jpg" data-background-size="contain"></section><section><h2>What is a model?</h2><p class="fragment"><b>HINT:</b> Not a UML diagram</p><p class="fragment">A set of services, entities and value objects expressed in classes, methods, and refs.</p></section><section><h2><a href="https://dry-python.org/">dry-python</a></h2><p>A set of libraries for pluggable business logic components.</p><img class="plain" src="https://raw.githubusercontent.com/dry-python/brand/master/logo/dry-python.png"></section><section data-background-image="ebc6682574f1147cb5360827813df21f.jpg" data-background-size="contain"></section><section><img class="plain" src="https://raw.githubusercontent.com/dry-python/brand/master/logo/stories.png"><p>Define a user story in the business transaction DSL.</p><p>Separate state, implementation and specification.</p></section><section><h2>Layout</h2><pre><code>project
├── admin
├── forms
├── migrations
├── models
├── <b>services</b>
├── templates
└── views</code></pre></section><section><h2>Specification DSL</h2><pre><code class="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</code></pre></section><section><h2>Steps implementation</h2><pre><code class="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()</code></pre></section><section><h2>Story Execution</h2><pre><code class="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</code></pre></section><section><h2>pros</h2><ol><li class="fragment">Clean flow in the source code</li><li class="fragment">Separate step implementation</li><li class="fragment">Each step knows nothing about a neighbor</li><li class="fragment">Easy reuse of code</li><li class="fragment">Allows to instrument code easily</li></ol></section><section data-background-image="41a288079292c670fc6adbb991c81b5d.png" data-background-size="contain"><h2>DEBUG TOOLBAR</h2><br><br><br><br><br><br><br><br><br><br></section><section data-background-image="2112306ed16594557ec48c5aadcb9840.png" data-background-size="contain"><h2 style="color: white">py.test</h2></section><section data-background-image="ea36d933df7211c5a711818f2145d767.png" data-background-size="contain"><h2>Sentry</h2></section><section data-background-image="c7436da758bca93b26c96ebec943822b.gif" data-background-size="contain"><h2>ELK</h2></section><section data-background-image="fe2a6f1002015746d4defff9ebec738d.jpg" data-background-size="contain"></section><section><img class="plain" src="369e6e115b4d64bf62027bd9af901ef5.png" height="150" style="padding-right: 70px;"><img class="plain" src="9300477553d4bcce7da193f93a2ebace.png" height="150" style="padding-right: 70px;"><ul><li class="fragment">We do not have the tooling to work with data</li><li class="fragment">There are no data contracts written in code</li></ul></section><section><img class="plain" src="d901adc64e26852387684441b0a3688f.png"></section><section><h2>Layout</h2><pre><code>project
├── admin
├── <b>aggregates</b>
├── forms
├── migrations
├── models
├── services
├── templates
└── views</code></pre></section><section><h2>dataclasses</h2><pre><code class="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]</code></pre></section><section><h2>State Contract</h2><pre><code class="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]</code></pre></section><section><h2>pros</h2><ol><li class="fragment">Explicit data contracts and relations in code</li><li class="fragment">Data store independent</li><li class="fragment">Catch errors when they occur</li><li class="fragment">Not when they propagate to exception</li></ol><h2 class="fragment">cons</h2><ol><li class="fragment">Working with data sources manually</li></ol></section><section data-background-image="127ce80f030ec2d257e80f1a89c43991.jpg" data-background-size="contain"></section><section><img class="plain" src="https://raw.githubusercontent.com/dry-python/brand/master/logo/mappers.png"><p>Declarative mappers from ORM models to domain entities. And back again!</p></section><section><h2>Layout</h2><pre><code>project
├── admin
├── aggregates
├── forms
├── migrations
├── models
├── <b>repositories</b>
├── services
├── templates
└── views</code></pre></section><section><h2>Django ORM</h2><pre><code class="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()</code></pre></section><section><h2>Swagger definitions</h2><pre><code class="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}")</code></pre></section><section><h2>GraphQL queries</h2><pre><code class="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}))</code></pre></section><section><img class="plain" src="b1b27de9e7945cf6f93f4bf45556f6eb.png"></section><section><h2>How we use third-party libraries</h2><pre><code class="python">from pusher import Pusher
class Purchase:
def send_purchase_notification(ctx):
Pusher().trigger("private-user-1")</code></pre><pre><code class="python">def test_before(monkeypatch):
monkeypatch.setattr(pusher, "Pusher", Mock())
# ...
pusher.Pusher.trigger.assert_called_once_with(
"private-user-1"
)</code></pre></section><section><img class="plain" src="0b397181aa5c6d6bf6fd699a445f76fc.png"></section><section><h2>How to use it with DI</h2><pre><code class="python">@dataclass
class Purchase:
def send_purchase_notification(ctx):
self.trigger_message(UserStream(ctx.user))
trigger_message: Emitter</code></pre><pre><code class="python">def test_after(emitter):
# ...
Purchase.trigger_message.assert_called_once_with(
UserStream(User(primary_key=1))
)</code></pre></section><section><img class="plain" src="https://raw.githubusercontent.com/dry-python/brand/master/logo/dependencies.png"><p>Provide composition instead of inheritance.</p><p>Solves top-down approach problem.</p></section><section><h2>Layout</h2><pre><code>project
├── admin
├── aggregates
├── forms
├── migrations
├── models
├── repositories
├── services
├── templates
├── <b>implementation</b>
└── views</code></pre></section><section><h2>Injection</h2><pre><code class="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)</code></pre></section><section data-background-image="1b48416ed8e37697b951b58bbab9cb3a.png" data-background-size="contain" data-background-color="#C10C06"></section><section><h2>Get in touch</h2><ul><li><a href="https://dry-python.org/">dry-python.org</a></li><li><a href="https://twitter.com/dry_py">twitter.com/dry_py</a></li><li><a href="https://github.com/dry-python">github.com/dry-python</a></li><li><a href="https://gitter.im/dry-python">gitter.im/dry-python</a></li></ul></section></div></div><script src="reveal.js"></script></body></html>