Repository navigation
Expand file tree
/
Copy pathchapter_06_uow.html
More file actions
1081 lines (1035 loc) · 67.2 KB
/
Copy pathchapter_06_uow.html
File metadata and controls
1081 lines (1035 loc) · 67.2 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.10">
<title>Unit of Work Pattern</title>
<style>
/* Asciidoctor default stylesheet | MIT License | https://asciidoctor.org */
@import url("//fonts.googleapis.com/css?family=Noto+Sans:300,600italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700");
@import url(//asciidoctor.org/stylesheets/asciidoctor.css); /* Default asciidoc style framework - important */
/* customisations by harry */
/* hide inline ditaa/plantuml source listings for images */
.image-source {
display: none
}
/* make formal codeblocks a bit nicer */
.exampleblock > .content {
padding: 2px;
background-color: white;
border: 0;
margin-bottom: 2em;
}
.exampleblock .title {
text-align: right;
}
/* end customisations by harry */
/* CUSTOMISATIONS */
/* Change the values in root for quick customisation. If you want even more fine grain... venture further. */
:root{
--maincolor:#FFFFFF;
--primarycolor:#2c3e50;
--secondarycolor:#ba3925;
--tertiarycolor: #186d7a;
--sidebarbackground:#CCC;
--linkcolor:#b71c1c;
--linkcoloralternate:#f44336;
--white:#FFFFFF;
--black:#000000;
}
/* Text styles */
h1{color:var(--primarycolor) !important;}
h2,h3,h4,h5,h6{color:var(--secondarycolor) !important;}
.title{color:var(--tertiarycolor) !important; font-family:"Noto Sans",sans-serif !important;font-style: normal !important; font-weight: normal !important;}
p{font-family: "Noto Sans",sans-serif !important}
/* Table styles */
th{font-family: "Noto Sans",sans-serif !important}
/* Responsiveness fixes */
video {
max-width: 100%;
}
@media all and (max-width: 600px) {
table {
width: 55vw!important;
font-size: 3vw;
}
</style>
</head>
<body class="article toc2 toc-left">
<div id="buy_the_book" style="position: absolute; top: 0; right: 0; z-index:100">
<a href="/#buy_the_book">
<img src="/images/buy_the_book.svg" alt="buy the book ribbon">
</a>
</div>
<div id="header">
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="/book/preface.html">Preface</a></li>
<li><a href="/book/introduction.html">Introduction</a></li>
<li><a href="/book/part1.html">Building an Architecture to Support Domain Modeling</a></li>
<li><a href="/book/chapter_01_domain_model.html">1. Domain Modeling</a></li>
<li><a href="/book/chapter_02_repository.html">2. Repository Pattern</a></li>
<li><a href="/book/chapter_03_abstractions.html">3. A Brief Interlude: On Coupling <span class="keep-together">and Abstractions</span></a></li>
<li><a href="/book/chapter_04_service_layer.html">4. Our First Use Case: <span class="keep-together">Flask API and Service Layer</span></a></li>
<li><a href="/book/chapter_05_high_gear_low_gear.html">5. TDD in High Gear and Low Gear</a></li>
<li><a href="/book/chapter_06_uow.html">6. Unit of Work Pattern</a></li>
<li><a href="/book/chapter_07_aggregate.html">7. Aggregates and Consistency Boundaries</a></li>
<li><a href="/book/part2.html">Event-Driven Architecture</a></li>
<li><a href="/book/chapter_08_events_and_message_bus.html">8. Events and the Message Bus</a></li>
<li><a href="/book/chapter_09_all_messagebus.html">9. Going to Town on the Message Bus</a></li>
<li><a href="/book/chapter_10_commands.html">10. Commands and Command Handler</a></li>
<li><a href="/book/chapter_11_external_events.html">11. Event-Driven Architecture: Using Events to Integrate Microservices</a></li>
<li><a href="/book/chapter_12_cqrs.html">12. Command-Query Responsibility Segregation (CQRS)</a></li>
<li><a href="/book/chapter_13_dependency_injection.html">13. Dependency Injection (and Bootstrapping)</a></li>
<li><a href="/book/epilogue_1_how_to_get_there_from_here.html">Appendix A: Epilogue</a></li>
<li><a href="/book/appendix_ds1_table.html">Appendix B: Summary Diagram and Table</a></li>
<li><a href="/book/appendix_project_structure.html">Appendix C: A Template Project Structure</a></li>
<li><a href="/book/appendix_csvs.html">Appendix D: Swapping Out the Infrastructure: <span class="keep-together">Do Everything with CSVs</span></a></li>
<li><a href="/book/appendix_django.html">Appendix E: Repository and Unit of Work <span class="keep-together">Patterns with Django</span></a></li>
<li><a href="/book/appendix_validation.html">Appendix F: Validation</a></li>
</ul>
</div>
</div>
<div id="content">
<div class="sect1">
<h2 id="chapter_06_uow">Unit of Work Pattern</h2>
<div class="sectionbody">
<div class="paragraph">
<p>In this chapter we’ll introduce the final piece of the puzzle that ties
together the Repository and Service Layer patterns: the <em>Unit of Work</em> pattern.</p>
</div>
<div class="paragraph">
<p>If the Repository pattern is our abstraction over the idea of persistent storage,
the Unit of Work (UoW) pattern is our abstraction over the idea of <em>atomic operations</em>. It
will allow us to finally and fully decouple our service layer from the data layer.</p>
</div>
<div class="paragraph">
<p><a href="#before_uow_diagram">Without UoW: API talks directly to three layers</a> shows that, currently, a lot of communication occurs
across the layers of our infrastructure: the API talks directly to the database
layer to start a session, it talks to the repository layer to initialize
<code>SQLAlchemyRepository</code>, and it talks to the service layer to ask it to allocate.</p>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
<div class="paragraph">
<p>The code for this chapter is in the
chapter_06_uow branch <a href="https://oreil.ly/MoWdZ">on <span class="keep-together">GitHub</span></a>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>git clone https://github.com/cosmicpython/code.git
cd code
git checkout chapter_06_uow
# or to code along, checkout Chapter 4:
git checkout chapter_04_service_layer</pre>
</div>
</div>
</td>
</tr>
</table>
</div>
<div id="before_uow_diagram" class="imageblock width-75">
<div class="content">
<img src="images/apwp_0601.png" alt="apwp 0601">
</div>
<div class="title">Figure 1. Without UoW: API talks directly to three layers</div>
</div>
<div class="paragraph">
<p><a href="#after_uow_diagram">With UoW: UoW now manages database state</a> shows our target state. The Flask API now does only two
things: it initializes a unit of work, and it invokes a service. The service
collaborates with the UoW (we like to think of the UoW as being part of the
service layer), but neither the service function itself nor Flask now needs
to talk directly to the database.</p>
</div>
<div class="paragraph">
<p>And we’ll do it all using a lovely piece of Python syntax, a context manager.</p>
</div>
<div id="after_uow_diagram" class="imageblock width-75">
<div class="content">
<img src="images/apwp_0602.png" alt="apwp 0602">
</div>
<div class="title">Figure 2. With UoW: UoW now manages database state</div>
</div>
<div class="sect2">
<h3 id="_the_unit_of_work_collaborates_with_the_repository">The Unit of Work Collaborates with the Repository</h3>
<div class="paragraph">
<p>Let’s see the unit of work (or UoW, which we pronounce "you-wow") in action. Here’s how the service layer will look when we’re finished:</p>
</div>
<div id="uow_preview" class="exampleblock">
<div class="title">Preview of unit of work in action (src/allocation/service_layer/services.py)</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">def</span> <span class="tok-nf">allocate</span><span class="tok-p">(</span>
<span class="tok-n">orderid</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">:</span> <span class="tok-nb">int</span><span class="tok-p">,</span>
<span class="tok-n">uow</span><span class="tok-p">:</span> <span class="tok-n">unit_of_work</span><span class="tok-o">.</span><span class="tok-n">AbstractUnitOfWork</span>
<span class="tok-p">)</span> <span class="tok-o">-></span> <span class="tok-nb">str</span><span class="tok-p">:</span>
<span class="tok-n">line</span> <span class="tok-o">=</span> <span class="tok-n">OrderLine</span><span class="tok-p">(</span><span class="tok-n">orderid</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">)</span>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span> #<b class="conum">(1)</b>
<span class="tok-n">batches</span> <span class="tok-o">=</span> <span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">batches</span><span class="tok-o">.</span><span class="tok-n">list</span><span class="tok-p">()</span> #<b class="conum">(2)</b>
<span class="tok-o">...</span>
<span class="tok-n">batchref</span> <span class="tok-o">=</span> <span class="tok-n">model</span><span class="tok-o">.</span><span class="tok-n">allocate</span><span class="tok-p">(</span><span class="tok-n">line</span><span class="tok-p">,</span> <span class="tok-n">batches</span><span class="tok-p">)</span>
<span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span> #<b class="conum">(3)</b></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p>We’ll start a UoW as a context manager.</p>
</li>
<li>
<p><code>uow.batches</code> is the batches repo, so the UoW provides us
access to our permanent storage.</p>
</li>
<li>
<p>When we’re done, we commit or roll back our work, using the UoW.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The UoW acts as a single entrypoint to our persistent storage, and it
keeps track of what objects were loaded and of the latest state.<sup class="footnote">[<a id="_footnoteref_1" class="footnote" href="#_footnotedef_1" title="View footnote.">1</a>]</sup></p>
</div>
<div class="paragraph">
<p>This gives us three useful things:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>A stable snapshot of the database to work with, so the
objects we use aren’t changing halfway through an operation</p>
</li>
<li>
<p>A way to persist all of our changes at once, so if something
goes wrong, we don’t end up in an inconsistent state</p>
</li>
<li>
<p>A simple API to our persistence concerns and a handy place
to get a repository</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="_test_driving_a_uow_with_integration_tests">Test-Driving a UoW with Integration Tests</h3>
<div class="paragraph">
<p>Here are our integration tests for the UOW:</p>
</div>
<div id="test_unit_of_work" class="exampleblock">
<div class="title">A basic "round-trip" test for a UoW (tests/integration/test_uow.py)</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">def</span> <span class="tok-nf">test_uow_can_retrieve_a_batch_and_allocate_to_it</span><span class="tok-p">(</span><span class="tok-n">session_factory</span><span class="tok-p">):</span>
<span class="tok-n">session</span> <span class="tok-o">=</span> <span class="tok-n">session_factory</span><span class="tok-p">()</span>
<span class="tok-n">insert_batch</span><span class="tok-p">(</span><span class="tok-n">session</span><span class="tok-p">,</span> <span class="tok-s1">'batch1'</span><span class="tok-p">,</span> <span class="tok-s1">'HIPSTER-WORKBENCH'</span><span class="tok-p">,</span> <span class="tok-mi">100</span><span class="tok-p">,</span> <span class="tok-bp">None</span><span class="tok-p">)</span>
<span class="tok-n">session</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span>
<span class="tok-n">uow</span> <span class="tok-o">=</span> <span class="tok-n">unit_of_work</span><span class="tok-o">.</span><span class="tok-n">SqlAlchemyUnitOfWork</span><span class="tok-p">(</span><span class="tok-n">session_factory</span><span class="tok-p">)</span> #<b class="conum">(1)</b>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span>
<span class="tok-n">batch</span> <span class="tok-o">=</span> <span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">batches</span><span class="tok-o">.</span><span class="tok-n">get</span><span class="tok-p">(</span><span class="tok-n">reference</span><span class="tok-o">=</span><span class="tok-s1">'batch1'</span><span class="tok-p">)</span> #<b class="conum">(2)</b>
<span class="tok-n">line</span> <span class="tok-o">=</span> <span class="tok-n">model</span><span class="tok-o">.</span><span class="tok-n">OrderLine</span><span class="tok-p">(</span><span class="tok-s1">'o1'</span><span class="tok-p">,</span> <span class="tok-s1">'HIPSTER-WORKBENCH'</span><span class="tok-p">,</span> <span class="tok-mi">10</span><span class="tok-p">)</span>
<span class="tok-n">batch</span><span class="tok-o">.</span><span class="tok-n">allocate</span><span class="tok-p">(</span><span class="tok-n">line</span><span class="tok-p">)</span>
<span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span> #<b class="conum">(3)</b>
<span class="tok-n">batchref</span> <span class="tok-o">=</span> <span class="tok-n">get_allocated_batch_ref</span><span class="tok-p">(</span><span class="tok-n">session</span><span class="tok-p">,</span> <span class="tok-s1">'o1'</span><span class="tok-p">,</span> <span class="tok-s1">'HIPSTER-WORKBENCH'</span><span class="tok-p">)</span>
<span class="tok-k">assert</span> <span class="tok-n">batchref</span> <span class="tok-o">==</span> <span class="tok-s1">'batch1'</span></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p>We initialize the UoW by using our custom session factory
and get back a <code>uow</code> object to use in our <code>with</code> block.</p>
</li>
<li>
<p>The UoW gives us access to the batches repository via
<code>uow.batches</code>.</p>
</li>
<li>
<p>We call <code>commit()</code> on it when we’re done.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>For the curious, the <code>insert_batch</code> and <code>get_allocated_batch_ref</code> helpers
look like this:</p>
</div>
<div id="sql_helpers" class="exampleblock">
<div class="title">Helpers for doing SQL stuff (tests/integration/test_uow.py)</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">def</span> <span class="tok-nf">insert_batch</span><span class="tok-p">(</span><span class="tok-n">session</span><span class="tok-p">,</span> <span class="tok-n">ref</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">,</span> <span class="tok-n">eta</span><span class="tok-p">):</span>
<span class="tok-n">session</span><span class="tok-o">.</span><span class="tok-n">execute</span><span class="tok-p">(</span>
<span class="tok-s1">'INSERT INTO batches (reference, sku, _purchased_quantity, eta)'</span>
<span class="tok-s1">' VALUES (:ref, :sku, :qty, :eta)'</span><span class="tok-p">,</span>
<span class="tok-nb">dict</span><span class="tok-p">(</span><span class="tok-n">ref</span><span class="tok-o">=</span><span class="tok-n">ref</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-o">=</span><span class="tok-n">sku</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-o">=</span><span class="tok-n">qty</span><span class="tok-p">,</span> <span class="tok-n">eta</span><span class="tok-o">=</span><span class="tok-n">eta</span><span class="tok-p">)</span>
<span class="tok-p">)</span>
<span class="tok-k">def</span> <span class="tok-nf">get_allocated_batch_ref</span><span class="tok-p">(</span><span class="tok-n">session</span><span class="tok-p">,</span> <span class="tok-n">orderid</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">):</span>
<span class="tok-p">[[</span><span class="tok-n">orderlineid</span><span class="tok-p">]]</span> <span class="tok-o">=</span> <span class="tok-n">session</span><span class="tok-o">.</span><span class="tok-n">execute</span><span class="tok-p">(</span>
<span class="tok-s1">'SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku'</span><span class="tok-p">,</span>
<span class="tok-nb">dict</span><span class="tok-p">(</span><span class="tok-n">orderid</span><span class="tok-o">=</span><span class="tok-n">orderid</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-o">=</span><span class="tok-n">sku</span><span class="tok-p">)</span>
<span class="tok-p">)</span>
<span class="tok-p">[[</span><span class="tok-n">batchref</span><span class="tok-p">]]</span> <span class="tok-o">=</span> <span class="tok-n">session</span><span class="tok-o">.</span><span class="tok-n">execute</span><span class="tok-p">(</span>
<span class="tok-s1">'SELECT b.reference FROM allocations JOIN batches AS b ON batch_id = b.id'</span>
<span class="tok-s1">' WHERE orderline_id=:orderlineid'</span><span class="tok-p">,</span>
<span class="tok-nb">dict</span><span class="tok-p">(</span><span class="tok-n">orderlineid</span><span class="tok-o">=</span><span class="tok-n">orderlineid</span><span class="tok-p">)</span>
<span class="tok-p">)</span>
<span class="tok-k">return</span> <span class="tok-n">batchref</span></code></pre>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_unit_of_work_and_its_context_manager">Unit of Work and Its Context Manager</h3>
<div class="paragraph">
<p>In our tests we’ve implicitly defined an interface for what a UoW needs to do. Let’s make that explicit by using an abstract
base class:</p>
</div>
<div id="abstract_unit_of_work" class="exampleblock">
<div class="title">Abstract UoW context manager (src/allocation/service_layer/unit_of_work.py)</div>
<div class="content">
<div class="listingblock skip">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">class</span> <span class="tok-nc">AbstractUnitOfWork</span><span class="tok-p">(</span><span class="tok-n">abc</span><span class="tok-o">.</span><span class="tok-n">ABC</span><span class="tok-p">):</span>
<span class="tok-n">batches</span><span class="tok-p">:</span> <span class="tok-n">repository</span><span class="tok-o">.</span><span class="tok-n">AbstractRepository</span> #<b class="conum">(1)</b>
<span class="tok-k">def</span> <span class="tok-fm">__exit__</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">,</span> <span class="tok-o">*</span><span class="tok-n">args</span><span class="tok-p">):</span> #<b class="conum">(2)</b>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">rollback</span><span class="tok-p">()</span> #<b class="conum">(4)</b>
<span class="tok-nd">@abc.abstractmethod</span>
<span class="tok-k">def</span> <span class="tok-nf">commit</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span> #<b class="conum">(3)</b>
<span class="tok-k">raise</span> <span class="tok-ne">NotImplementedError</span>
<span class="tok-nd">@abc.abstractmethod</span>
<span class="tok-k">def</span> <span class="tok-nf">rollback</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span> #<b class="conum">(4)</b>
<span class="tok-k">raise</span> <span class="tok-ne">NotImplementedError</span></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p>The UoW provides an attribute called <code>.batches</code>, which will give us access
to the batches repository.</p>
</li>
<li>
<p>If you’ve never seen a context manager, <code>__enter__</code> and <code>__exit__</code> are
the two magic methods that execute when we enter the <code>with</code> block and
when we exit it, respectively. They’re our setup and teardown phases.</p>
</li>
<li>
<p>We’ll call this method to explicitly commit our work when we’re ready.</p>
</li>
<li>
<p>If we don’t commit, or if we exit the context manager by raising an error,
we do a <code>rollback</code>. (The rollback has no effect if <code>commit()</code> has been
called. Read on for more discussion of this.)</p>
</li>
</ol>
</div>
<div class="sect3">
<h4 id="_the_real_unit_of_work_uses_sqlalchemy_sessions">The Real Unit of Work Uses SQLAlchemy Sessions</h4>
<div class="paragraph">
<p>The main thing that our concrete implementation adds is the
database session:</p>
</div>
<div id="unit_of_work" class="exampleblock">
<div class="title">The real SQLAlchemy UoW (src/allocation/service_layer/unit_of_work.py)</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-n">DEFAULT_SESSION_FACTORY</span> <span class="tok-o">=</span> <span class="tok-n">sessionmaker</span><span class="tok-p">(</span><span class="tok-n">bind</span><span class="tok-o">=</span><span class="tok-n">create_engine</span><span class="tok-p">(</span> #<b class="conum">(1)</b>
<span class="tok-n">config</span><span class="tok-o">.</span><span class="tok-n">get_postgres_uri</span><span class="tok-p">(),</span>
<span class="tok-p">))</span>
<span class="tok-k">class</span> <span class="tok-nc">SqlAlchemyUnitOfWork</span><span class="tok-p">(</span><span class="tok-n">AbstractUnitOfWork</span><span class="tok-p">):</span>
<span class="tok-k">def</span> <span class="tok-fm">__init__</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">,</span> <span class="tok-n">session_factory</span><span class="tok-o">=</span><span class="tok-n">DEFAULT_SESSION_FACTORY</span><span class="tok-p">):</span>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">session_factory</span> <span class="tok-o">=</span> <span class="tok-n">session_factory</span> #<b class="conum">(1)</b>
<span class="tok-k">def</span> <span class="tok-fm">__enter__</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">session</span> <span class="tok-o">=</span> <span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">session_factory</span><span class="tok-p">()</span> <span class="tok-c1"># type: Session </span>#<b class="conum">(2)</b>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">batches</span> <span class="tok-o">=</span> <span class="tok-n">repository</span><span class="tok-o">.</span><span class="tok-n">SqlAlchemyRepository</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">session</span><span class="tok-p">)</span> #<b class="conum">(2)</b>
<span class="tok-k">return</span> <span class="tok-nb">super</span><span class="tok-p">()</span><span class="tok-o">.</span><span class="tok-fm">__enter__</span><span class="tok-p">()</span>
<span class="tok-k">def</span> <span class="tok-fm">__exit__</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">,</span> <span class="tok-o">*</span><span class="tok-n">args</span><span class="tok-p">):</span>
<span class="tok-nb">super</span><span class="tok-p">()</span><span class="tok-o">.</span><span class="tok-fm">__exit__</span><span class="tok-p">(</span><span class="tok-o">*</span><span class="tok-n">args</span><span class="tok-p">)</span>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">session</span><span class="tok-o">.</span><span class="tok-n">close</span><span class="tok-p">()</span> #<b class="conum">(3)</b>
<span class="tok-k">def</span> <span class="tok-nf">commit</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span> #<b class="conum">(4)</b>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">session</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span>
<span class="tok-k">def</span> <span class="tok-nf">rollback</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span> #<b class="conum">(4)</b>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">session</span><span class="tok-o">.</span><span class="tok-n">rollback</span><span class="tok-p">()</span></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p>The module defines a default session factory that will connect to Postgres,
but we allow that to be overridden in our integration tests so that we
can use SQLite instead.</p>
</li>
<li>
<p>The <code>__enter__</code> method is responsible for starting a database session and instantiating
a real repository that can use that session.</p>
</li>
<li>
<p>We close the session on exit.</p>
</li>
<li>
<p>Finally, we provide concrete <code>commit()</code> and <code>rollback()</code> methods that
use our database session.</p>
</li>
</ol>
</div>
</div>
<div class="sect3">
<h4 id="_fake_unit_of_work_for_testing">Fake Unit of Work for Testing</h4>
<div class="paragraph">
<p>Here’s how we use a fake UoW in our service-layer tests:</p>
</div>
<div id="fake_unit_of_work" class="exampleblock">
<div class="title">Fake UoW (tests/unit/test_services.py)</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">class</span> <span class="tok-nc">FakeUnitOfWork</span><span class="tok-p">(</span><span class="tok-n">unit_of_work</span><span class="tok-o">.</span><span class="tok-n">AbstractUnitOfWork</span><span class="tok-p">):</span>
<span class="tok-k">def</span> <span class="tok-fm">__init__</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">batches</span> <span class="tok-o">=</span> <span class="tok-n">FakeRepository</span><span class="tok-p">([])</span> #<b class="conum">(1)</b>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">committed</span> <span class="tok-o">=</span> <span class="tok-bp">False</span> #<b class="conum">(2)</b>
<span class="tok-k">def</span> <span class="tok-nf">commit</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">committed</span> <span class="tok-o">=</span> <span class="tok-bp">True</span> #<b class="conum">(2)</b>
<span class="tok-k">def</span> <span class="tok-nf">rollback</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span>
<span class="tok-k">pass</span>
<span class="tok-k">def</span> <span class="tok-nf">test_add_batch</span><span class="tok-p">():</span>
<span class="tok-n">uow</span> <span class="tok-o">=</span> <span class="tok-n">FakeUnitOfWork</span><span class="tok-p">()</span> #<b class="conum">(3)</b>
<span class="tok-n">services</span><span class="tok-o">.</span><span class="tok-n">add_batch</span><span class="tok-p">(</span><span class="tok-s2">"b1"</span><span class="tok-p">,</span> <span class="tok-s2">"CRUNCHY-ARMCHAIR"</span><span class="tok-p">,</span> <span class="tok-mi">100</span><span class="tok-p">,</span> <span class="tok-bp">None</span><span class="tok-p">,</span> <span class="tok-n">uow</span><span class="tok-p">)</span> #<b class="conum">(3)</b>
<span class="tok-k">assert</span> <span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">batches</span><span class="tok-o">.</span><span class="tok-n">get</span><span class="tok-p">(</span><span class="tok-s2">"b1"</span><span class="tok-p">)</span> <span class="tok-ow">is</span> <span class="tok-ow">not</span> <span class="tok-bp">None</span>
<span class="tok-k">assert</span> <span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">committed</span>
<span class="tok-k">def</span> <span class="tok-nf">test_allocate_returns_allocation</span><span class="tok-p">():</span>
<span class="tok-n">uow</span> <span class="tok-o">=</span> <span class="tok-n">FakeUnitOfWork</span><span class="tok-p">()</span> #<b class="conum">(3)</b>
<span class="tok-n">services</span><span class="tok-o">.</span><span class="tok-n">add_batch</span><span class="tok-p">(</span><span class="tok-s2">"batch1"</span><span class="tok-p">,</span> <span class="tok-s2">"COMPLICATED-LAMP"</span><span class="tok-p">,</span> <span class="tok-mi">100</span><span class="tok-p">,</span> <span class="tok-bp">None</span><span class="tok-p">,</span> <span class="tok-n">uow</span><span class="tok-p">)</span> #<b class="conum">(3)</b>
<span class="tok-n">result</span> <span class="tok-o">=</span> <span class="tok-n">services</span><span class="tok-o">.</span><span class="tok-n">allocate</span><span class="tok-p">(</span><span class="tok-s2">"o1"</span><span class="tok-p">,</span> <span class="tok-s2">"COMPLICATED-LAMP"</span><span class="tok-p">,</span> <span class="tok-mi">10</span><span class="tok-p">,</span> <span class="tok-n">uow</span><span class="tok-p">)</span> #<b class="conum">(3)</b>
<span class="tok-k">assert</span> <span class="tok-n">result</span> <span class="tok-o">==</span> <span class="tok-s2">"batch1"</span>
<span class="tok-o">...</span></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p><code>FakeUnitOfWork</code> and <code>FakeRepository</code> are tightly coupled,
just like the real <code>UnitofWork</code> and <code>Repository</code> classes.
That’s fine because we recognize that the objects are collaborators.</p>
</li>
<li>
<p>Notice the similarity with the fake <code>commit()</code> function
from <code>FakeSession</code> (which we can now get rid of). But it’s
a substantial improvement because we’re now <span class="keep-together">faking</span> out
code that we wrote rather than third-party code. Some
people say, <a href="https://oreil.ly/0LVj3">"Don’t mock what you don’t own"</a>.</p>
</li>
<li>
<p>In our tests, we can instantiate a UoW and pass it to
our service layer, rather than passing a repository and a session.
This is considerably less cumbersome.</p>
</li>
</ol>
</div>
<div class="sidebarblock nobreakinside less_space">
<div class="content">
<div class="title">Don’t Mock What You Don’t Own</div>
<div class="paragraph">
<p>Why do we feel more comfortable mocking the UoW than the session?
Both of our fakes achieve the same thing: they give us a way to swap out our
persistence layer so we can run tests in memory instead of needing to
talk to a real database. The difference is in the resulting design.</p>
</div>
<div class="paragraph">
<p>If we cared only about writing tests that run quickly, we could create mocks
that replace SQLAlchemy and use those throughout our codebase. The problem is
that <code>Session</code> is a complex object that exposes lots of persistence-related
functionality. It’s easy to use <code>Session</code> to make arbitrary queries against
the database, but that quickly leads to data access code being sprinkled all
over the codebase. To avoid that, we want to limit access to our persistence
layer so each component has exactly what it needs and nothing more.</p>
</div>
<div class="paragraph">
<p>By coupling to the <code>Session</code> interface, you’re choosing to couple to all the
complexity of SQLAlchemy. Instead, we want to choose a simpler abstraction and
use that to clearly separate responsibilities. Our UoW is much simpler
than a session, and we feel comfortable with the service layer being able to
start and stop units of work.</p>
</div>
<div class="paragraph">
<p>"Don’t mock what you don’t own" is a rule of thumb that forces us to build
these simple abstractions over messy subsystems. This has the same performance
benefit as mocking the SQLAlchemy session but encourages us to think carefully
about our designs.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_using_the_uow_in_the_service_layer">Using the UoW in the Service Layer</h3>
<div class="paragraph">
<p>Here’s what our new service layer looks like:</p>
</div>
<div id="service_layer_with_uow" class="exampleblock">
<div class="title">Service layer using UoW (src/allocation/service_layer/services.py)</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">def</span> <span class="tok-nf">add_batch</span><span class="tok-p">(</span>
<span class="tok-n">ref</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">:</span> <span class="tok-nb">int</span><span class="tok-p">,</span> <span class="tok-n">eta</span><span class="tok-p">:</span> <span class="tok-n">Optional</span><span class="tok-p">[</span><span class="tok-n">date</span><span class="tok-p">],</span>
<span class="tok-n">uow</span><span class="tok-p">:</span> <span class="tok-n">unit_of_work</span><span class="tok-o">.</span><span class="tok-n">AbstractUnitOfWork</span> #<b class="conum">(1)</b>
<span class="tok-p">):</span>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span>
<span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">batches</span><span class="tok-o">.</span><span class="tok-n">add</span><span class="tok-p">(</span><span class="tok-n">model</span><span class="tok-o">.</span><span class="tok-n">Batch</span><span class="tok-p">(</span><span class="tok-n">ref</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">,</span> <span class="tok-n">eta</span><span class="tok-p">))</span>
<span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span>
<span class="tok-k">def</span> <span class="tok-nf">allocate</span><span class="tok-p">(</span>
<span class="tok-n">orderid</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">:</span> <span class="tok-nb">int</span><span class="tok-p">,</span>
<span class="tok-n">uow</span><span class="tok-p">:</span> <span class="tok-n">unit_of_work</span><span class="tok-o">.</span><span class="tok-n">AbstractUnitOfWork</span> #<b class="conum">(1)</b>
<span class="tok-p">)</span> <span class="tok-o">-></span> <span class="tok-nb">str</span><span class="tok-p">:</span>
<span class="tok-n">line</span> <span class="tok-o">=</span> <span class="tok-n">OrderLine</span><span class="tok-p">(</span><span class="tok-n">orderid</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">)</span>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span>
<span class="tok-n">batches</span> <span class="tok-o">=</span> <span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">batches</span><span class="tok-o">.</span><span class="tok-n">list</span><span class="tok-p">()</span>
<span class="tok-k">if</span> <span class="tok-ow">not</span> <span class="tok-n">is_valid_sku</span><span class="tok-p">(</span><span class="tok-n">line</span><span class="tok-o">.</span><span class="tok-n">sku</span><span class="tok-p">,</span> <span class="tok-n">batches</span><span class="tok-p">):</span>
<span class="tok-k">raise</span> <span class="tok-n">InvalidSku</span><span class="tok-p">(</span><span class="tok-n">f</span><span class="tok-s1">'Invalid sku {line.sku}'</span><span class="tok-p">)</span>
<span class="tok-n">batchref</span> <span class="tok-o">=</span> <span class="tok-n">model</span><span class="tok-o">.</span><span class="tok-n">allocate</span><span class="tok-p">(</span><span class="tok-n">line</span><span class="tok-p">,</span> <span class="tok-n">batches</span><span class="tok-p">)</span>
<span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span>
<span class="tok-k">return</span> <span class="tok-n">batchref</span></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p>Our service layer now has only the one dependency, once again
on an <em>abstract</em> UoW.</p>
</li>
</ol>
</div>
</div>
<div class="sect2">
<h3 id="_explicit_tests_for_commitrollback_behavior">Explicit Tests for Commit/Rollback Behavior</h3>
<div class="paragraph">
<p>To convince ourselves that the commit/rollback behavior works, we wrote
a couple of tests:</p>
</div>
<div id="testing_rollback" class="exampleblock">
<div class="title">Integration tests for rollback behavior (tests/integration/test_uow.py)</div>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">def</span> <span class="tok-nf">test_rolls_back_uncommitted_work_by_default</span><span class="tok-p">(</span><span class="tok-n">session_factory</span><span class="tok-p">):</span>
<span class="tok-n">uow</span> <span class="tok-o">=</span> <span class="tok-n">unit_of_work</span><span class="tok-o">.</span><span class="tok-n">SqlAlchemyUnitOfWork</span><span class="tok-p">(</span><span class="tok-n">session_factory</span><span class="tok-p">)</span>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span>
<span class="tok-n">insert_batch</span><span class="tok-p">(</span><span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">session</span><span class="tok-p">,</span> <span class="tok-s1">'batch1'</span><span class="tok-p">,</span> <span class="tok-s1">'MEDIUM-PLINTH'</span><span class="tok-p">,</span> <span class="tok-mi">100</span><span class="tok-p">,</span> <span class="tok-bp">None</span><span class="tok-p">)</span>
<span class="tok-n">new_session</span> <span class="tok-o">=</span> <span class="tok-n">session_factory</span><span class="tok-p">()</span>
<span class="tok-n">rows</span> <span class="tok-o">=</span> <span class="tok-nb">list</span><span class="tok-p">(</span><span class="tok-n">new_session</span><span class="tok-o">.</span><span class="tok-n">execute</span><span class="tok-p">(</span><span class="tok-s1">'SELECT * FROM "batches"'</span><span class="tok-p">))</span>
<span class="tok-k">assert</span> <span class="tok-n">rows</span> <span class="tok-o">==</span> <span class="tok-p">[]</span>
<span class="tok-k">def</span> <span class="tok-nf">test_rolls_back_on_error</span><span class="tok-p">(</span><span class="tok-n">session_factory</span><span class="tok-p">):</span>
<span class="tok-k">class</span> <span class="tok-nc">MyException</span><span class="tok-p">(</span><span class="tok-ne">Exception</span><span class="tok-p">):</span>
<span class="tok-k">pass</span>
<span class="tok-n">uow</span> <span class="tok-o">=</span> <span class="tok-n">unit_of_work</span><span class="tok-o">.</span><span class="tok-n">SqlAlchemyUnitOfWork</span><span class="tok-p">(</span><span class="tok-n">session_factory</span><span class="tok-p">)</span>
<span class="tok-k">with</span> <span class="tok-n">pytest</span><span class="tok-o">.</span><span class="tok-n">raises</span><span class="tok-p">(</span><span class="tok-n">MyException</span><span class="tok-p">):</span>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span>
<span class="tok-n">insert_batch</span><span class="tok-p">(</span><span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">session</span><span class="tok-p">,</span> <span class="tok-s1">'batch1'</span><span class="tok-p">,</span> <span class="tok-s1">'LARGE-FORK'</span><span class="tok-p">,</span> <span class="tok-mi">100</span><span class="tok-p">,</span> <span class="tok-bp">None</span><span class="tok-p">)</span>
<span class="tok-k">raise</span> <span class="tok-n">MyException</span><span class="tok-p">()</span>
<span class="tok-n">new_session</span> <span class="tok-o">=</span> <span class="tok-n">session_factory</span><span class="tok-p">()</span>
<span class="tok-n">rows</span> <span class="tok-o">=</span> <span class="tok-nb">list</span><span class="tok-p">(</span><span class="tok-n">new_session</span><span class="tok-o">.</span><span class="tok-n">execute</span><span class="tok-p">(</span><span class="tok-s1">'SELECT * FROM "batches"'</span><span class="tok-p">))</span>
<span class="tok-k">assert</span> <span class="tok-n">rows</span> <span class="tok-o">==</span> <span class="tok-p">[]</span></code></pre>
</div>
</div>
</div>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
We haven’t shown it here, but it can be worth testing some of the more
"obscure" database behavior, like transactions, against the "real"
database—that is, the same engine. For now, we’re getting away with using
SQLite instead of Postgres, but in <a href="/book/chapter_07_aggregate.html">[chapter_07_aggregate]</a>, we’ll switch
some of the tests to using the real database. It’s convenient that our UoW
class makes that easy!
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="_explicit_versus_implicit_commits">Explicit Versus Implicit Commits</h3>
<div class="paragraph">
<p>Now we briefly digress on different ways of implementing the UoW pattern.</p>
</div>
<div class="paragraph">
<p>We could imagine a slightly different version of the UoW that commits by default
and rolls back only if it spots an exception:</p>
</div>
<div id="uow_implicit_commit" class="exampleblock">
<div class="title">A UoW with implicit commit…​ (src/allocation/unit_of_work.py)</div>
<div class="content">
<div class="listingblock skip">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span>
<span class="tok-k">class</span> <span class="tok-nc">AbstractUnitOfWork</span><span class="tok-p">(</span><span class="tok-n">abc</span><span class="tok-o">.</span><span class="tok-n">ABC</span><span class="tok-p">):</span>
<span class="tok-k">def</span> <span class="tok-fm">__enter__</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">):</span>
<span class="tok-k">return</span> <span class="tok-bp">self</span>
<span class="tok-k">def</span> <span class="tok-fm">__exit__</span><span class="tok-p">(</span><span class="tok-bp">self</span><span class="tok-p">,</span> <span class="tok-n">exn_type</span><span class="tok-p">,</span> <span class="tok-n">exn_value</span><span class="tok-p">,</span> <span class="tok-n">traceback</span><span class="tok-p">):</span>
<span class="tok-k">if</span> <span class="tok-n">exn_type</span> <span class="tok-ow">is</span> <span class="tok-bp">None</span><span class="tok-p">:</span>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span> #<b class="conum">(1)</b>
<span class="tok-k">else</span><span class="tok-p">:</span>
<span class="tok-bp">self</span><span class="tok-o">.</span><span class="tok-n">rollback</span><span class="tok-p">()</span> #<b class="conum">(2)</b></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p>Should we have an implicit commit in the happy path?</p>
</li>
<li>
<p>And roll back only on exception?</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>It would allow us to save a line of code and to remove the explicit commit from our
client code:</p>
</div>
<div id="add_batch_nocommit" class="exampleblock">
<div class="title">...would save us a line of code (src/allocation/service_layer/services.py)</div>
<div class="content">
<div class="listingblock skip">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">def</span> <span class="tok-nf">add_batch</span><span class="tok-p">(</span><span class="tok-n">ref</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">:</span> <span class="tok-nb">int</span><span class="tok-p">,</span> <span class="tok-n">eta</span><span class="tok-p">:</span> <span class="tok-n">Optional</span><span class="tok-p">[</span><span class="tok-n">date</span><span class="tok-p">],</span> <span class="tok-n">uow</span><span class="tok-p">):</span>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span>
<span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">batches</span><span class="tok-o">.</span><span class="tok-n">add</span><span class="tok-p">(</span><span class="tok-n">model</span><span class="tok-o">.</span><span class="tok-n">Batch</span><span class="tok-p">(</span><span class="tok-n">ref</span><span class="tok-p">,</span> <span class="tok-n">sku</span><span class="tok-p">,</span> <span class="tok-n">qty</span><span class="tok-p">,</span> <span class="tok-n">eta</span><span class="tok-p">))</span>
<span class="tok-c1"># uow.commit()</span></code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>This is a judgment call, but we tend to prefer requiring the explicit commit
so that we have to choose when to flush state.</p>
</div>
<div class="paragraph">
<p>Although we use an extra line of code, this makes the software safe by default.
The default behavior is to <em>not change anything</em>. In turn, that makes our code
easier to reason about because there’s only one code path that leads to changes
in the system: total success and an explicit commit. Any other code path, any
exception, any early exit from the UoW’s scope leads to a safe state.</p>
</div>
<div class="paragraph">
<p>Similarly, we prefer to roll back by default because
it’s easier to understand; this rolls back to the last commit,
so either the user did one, or we blow their changes away. Harsh but simple.</p>
</div>
</div>
<div class="sect2">
<h3 id="_examples_using_uow_to_group_multiple_operations_into_an_atomic_unit">Examples: Using UoW to Group Multiple Operations into an Atomic Unit</h3>
<div class="paragraph">
<p>Here are a few examples showing the Unit of Work pattern in use. You can
see how it leads to simple reasoning about what blocks of code happen
together.</p>
</div>
<div class="sect3">
<h4 id="_example_1_reallocate">Example 1: Reallocate</h4>
<div class="paragraph">
<p>Suppose we want to be able to deallocate and then reallocate orders:</p>
</div>
<div id="reallocate" class="exampleblock">
<div class="title">Reallocate service function</div>
<div class="content">
<div class="listingblock skip">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">def</span> <span class="tok-nf">reallocate</span><span class="tok-p">(</span><span class="tok-n">line</span><span class="tok-p">:</span> <span class="tok-n">OrderLine</span><span class="tok-p">,</span> <span class="tok-n">uow</span><span class="tok-p">:</span> <span class="tok-n">AbstractUnitOfWork</span><span class="tok-p">)</span> <span class="tok-o">-></span> <span class="tok-nb">str</span><span class="tok-p">:</span>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span>
<span class="tok-n">batch</span> <span class="tok-o">=</span> <span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">batches</span><span class="tok-o">.</span><span class="tok-n">get</span><span class="tok-p">(</span><span class="tok-n">sku</span><span class="tok-o">=</span><span class="tok-n">line</span><span class="tok-o">.</span><span class="tok-n">sku</span><span class="tok-p">)</span>
<span class="tok-k">if</span> <span class="tok-n">batch</span> <span class="tok-ow">is</span> <span class="tok-bp">None</span><span class="tok-p">:</span>
<span class="tok-k">raise</span> <span class="tok-n">InvalidSku</span><span class="tok-p">(</span><span class="tok-n">f</span><span class="tok-s1">'Invalid sku {line.sku}'</span><span class="tok-p">)</span>
<span class="tok-n">batch</span><span class="tok-o">.</span><span class="tok-n">deallocate</span><span class="tok-p">(</span><span class="tok-n">line</span><span class="tok-p">)</span> #<b class="conum">(1)</b>
<span class="tok-n">allocate</span><span class="tok-p">(</span><span class="tok-n">line</span><span class="tok-p">)</span> #<b class="conum">(2)</b>
<span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p>If <code>deallocate()</code> fails, we don’t want to call <code>allocate()</code>, obviously.</p>
</li>
<li>
<p>If <code>allocate()</code> fails, we probably don’t want to actually commit
the <code>deallocate()</code> either.</p>
</li>
</ol>
</div>
</div>
<div class="sect3">
<h4 id="_example_2_change_batch_quantity">Example 2: Change Batch Quantity</h4>
<div class="paragraph">
<p>Our shipping company gives us a call to say that one of the container doors
opened, and half our sofas have fallen into the Indian Ocean. Oops!</p>
</div>
<div id="change_batch_quantity" class="exampleblock">
<div class="title">Change quantity</div>
<div class="content">
<div class="listingblock skip">
<div class="content">
<pre class="pygments highlight"><code data-lang="python"><span></span><span class="tok-k">def</span> <span class="tok-nf">change_batch_quantity</span><span class="tok-p">(</span><span class="tok-n">batchref</span><span class="tok-p">:</span> <span class="tok-nb">str</span><span class="tok-p">,</span> <span class="tok-n">new_qty</span><span class="tok-p">:</span> <span class="tok-nb">int</span><span class="tok-p">,</span> <span class="tok-n">uow</span><span class="tok-p">:</span> <span class="tok-n">AbstractUnitOfWork</span><span class="tok-p">):</span>
<span class="tok-k">with</span> <span class="tok-n">uow</span><span class="tok-p">:</span>
<span class="tok-n">batch</span> <span class="tok-o">=</span> <span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">batches</span><span class="tok-o">.</span><span class="tok-n">get</span><span class="tok-p">(</span><span class="tok-n">reference</span><span class="tok-o">=</span><span class="tok-n">batchref</span><span class="tok-p">)</span>
<span class="tok-n">batch</span><span class="tok-o">.</span><span class="tok-n">change_purchased_quantity</span><span class="tok-p">(</span><span class="tok-n">new_qty</span><span class="tok-p">)</span>
<span class="tok-k">while</span> <span class="tok-n">batch</span><span class="tok-o">.</span><span class="tok-n">available_quantity</span> <span class="tok-o"><</span> <span class="tok-mi">0</span><span class="tok-p">:</span>
<span class="tok-n">line</span> <span class="tok-o">=</span> <span class="tok-n">batch</span><span class="tok-o">.</span><span class="tok-n">deallocate_one</span><span class="tok-p">()</span> #<b class="conum">(1)</b>
<span class="tok-n">uow</span><span class="tok-o">.</span><span class="tok-n">commit</span><span class="tok-p">()</span></code></pre>
</div>
</div>
</div>
</div>
<div class="colist arabic">
<ol>
<li>
<p>Here we may need to deallocate any number of lines. If we get a failure
at any stage, we probably want to commit none of the changes.</p>
</li>
</ol>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_tidying_up_the_integration_tests">Tidying Up the Integration Tests</h3>
<div class="paragraph">
<p>We now have three sets of tests, all essentially pointing at the database:
<em>test_orm.py</em>, <em>test_repository.py</em>, and <em>test_uow.py</em>. Should we throw any
away?</p>
</div>
<div class="exampleblock">
<div class="content">
<div class="listingblock tree">
<div class="content">
<pre class="pygments highlight"><code data-lang="text"><span></span>└── tests
├── conftest.py
├── e2e
│   └── test_api.py
├── integration
│   ├── test_orm.py
│   ├── test_repository.py
│   └── test_uow.py
├── pytest.ini
└── unit
├── test_allocate.py
├── test_batches.py
└── test_services.py</code></pre>
</div>
</div>
</div>
</div>
<div class="paragraph">
<p>You should always feel free to throw away tests if you think they’re not going to
add value longer term. We’d say that <em>test_orm.py</em> was primarily a tool to help
us learn SQLAlchemy, so we won’t need that long term, especially if the main things
it’s doing are covered in <em>test_repository.py</em>. That last test, you might keep around,
but we could certainly see an argument for just keeping everything at the highest
possible level of abstraction (just as we did for the unit tests).</p>
</div>
<div class="sidebarblock nobreakinside less_space">
<div class="content">
<div class="title">Exercise for the Reader</div>
<div class="paragraph">
<p>For this chapter, probably the best thing to try is to implement a
UoW from scratch. The code, as always, is <a href="https://github.com/cosmicpython/code/tree/chapter_06_uow_exercise">on GitHub</a>. You could either follow the model we have quite closely,
or perhaps experiment with separating the UoW (whose responsibilities are
<code>commit()</code>, <code>rollback()</code>, and providing the <code>.batches</code> repository) from the
context manager, whose job is to initialize things, and then do the commit
or rollback on exit. If you feel like going all-functional rather than
messing about with all these classes, you could use <code>@contextmanager</code> from
<code>contextlib</code>.</p>
</div>
<div class="paragraph">
<p>We’ve stripped out both the actual UoW and the fakes, as well as paring back
the abstract UoW. Why not send us a link to your repo if you come up with
something you’re particularly proud of?</p>
</div>
</div>
</div>
<div class="admonitionblock tip">
<table>
<tr>
<td class="icon">
<div class="title">Tip</div>
</td>
<td class="content">
This is another example of the lesson from <a href="/book/chapter_05_high_gear_low_gear.html">[chapter_05_high_gear_low_gear]</a>:
as we build better abstractions, we can move our tests to run against them,
which leaves us free to change the underlying details.
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="_wrap_up">Wrap-Up</h3>
<div class="paragraph">
<p>Hopefully we’ve convinced you that the Unit of Work pattern is useful, and
that the context manager is a really nice Pythonic way
of visually grouping code into blocks that we want to happen atomically.</p>
</div>
<div class="paragraph">
<p>This pattern is so useful, in fact, that SQLAlchemy already uses a UoW
in the shape of the <code>Session</code> object. The <code>Session</code> object in SQLAlchemy is the way
that your application loads data from the database.</p>
</div>
<div class="paragraph">
<p>Every time you load a new entity from the database, the session begins to <em>track</em>
changes to the entity, and when the session is <em>flushed</em>, all your changes are
persisted together. Why do we go to the effort of abstracting away the SQLAlchemy session if it already implements the pattern we want?</p>
</div>
<div class="paragraph">
<p><a href="#chapter_06_uow_tradeoffs">Unit of Work pattern: the trade-offs</a> discusses some of the trade-offs.</p>
</div>
<table id="chapter_06_uow_tradeoffs" class="tableblock frame-all grid-all stretch">
<caption class="title">Table 1. Unit of Work pattern: the trade-offs</caption>
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Pros</th>
<th class="tableblock halign-left valign-top">Cons</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><div class="content"><div class="ulist">
<ul>
<li>
<p>We have a nice abstraction over the concept of atomic operations, and the
context manager makes it easy to see, visually, what blocks of code are
grouped together atomically.</p>
</li>
<li>
<p>We have explicit control over when a transaction starts and finishes, and our
application fails in a way that is safe by default. We never have to worry
that an operation is partially committed.</p>
</li>
<li>
<p>It’s a nice place to put all your repositories so client code can access them.</p>
</li>
<li>
<p>As you’ll see in later chapters, atomicity isn’t only about transactions; it
can help us work with events and the message bus.</p>
</li>
</ul>
</div></div></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="ulist">
<ul>
<li>
<p>Your ORM probably already has some perfectly good abstractions around
atomicity. SQLAlchemy even has context managers. You can go a long way
just passing a session around.</p>
</li>
<li>
<p>We’ve made it look easy, but you have to think quite carefully about
things like rollbacks, multithreading, and nested transactions. Perhaps just
sticking to what Django or Flask-SQLAlchemy gives you will keep your life
simpler.</p>
</li>
</ul>
</div></div></td>
</tr>
</tbody>
</table>
<div class="paragraph">
<p>For one thing, the Session API is rich and supports operations that we don’t
want or need in our domain. Our <code>UnitOfWork</code> simplifies the session to its
essential core: it can be started, committed, or thrown away.</p>
</div>
<div class="paragraph">
<p>For another, we’re using the <code>UnitOfWork</code> to access our <code>Repository</code> objects.
This is a neat bit of developer usability that we couldn’t do with a plain
SQLAlchemy <code>Session</code>.</p>
</div>
<div class="sidebarblock nobreakinside less_space">
<div class="content">
<div class="title">Unit of Work Pattern Recap</div>
<div class="dlist">
<dl>
<dt class="hdlist1">The Unit of Work pattern is an abstraction around data integrity</dt>
<dd>
<p>It helps to enforce the consistency of our domain model, and improves
performance, by letting us perform a single <em>flush</em> operation at the
end of an operation.</p>
</dd>
<dt class="hdlist1">It works closely with the Repository and Service Layer patterns</dt>
<dd>
<p>The Unit of Work pattern completes our abstractions over data access by
representing atomic updates. Each of our service-layer use cases runs in a
single unit of work that succeeds or fails as a block.</p>
</dd>
<dt class="hdlist1">This is a lovely case for a context manager</dt>
<dd>
<p>Context managers are an idiomatic way of defining scope in Python. We can use a
context manager to automatically roll back our work at the end of a request,
which means the system is safe by default.</p>
</dd>
<dt class="hdlist1">SQLAlchemy already implements this pattern</dt>
<dd>
<p>We introduce an even simpler abstraction over the SQLAlchemy <code>Session</code> object
in order to "narrow" the interface between the ORM and our code. This helps
to keep us loosely coupled.</p>
</dd>
</dl>
</div>
</div>
</div>
<div class="paragraph">
<p>Lastly, we’re motivated again by the dependency inversion principle: our
service layer depends on a thin abstraction, and we attach a concrete
implementation at the outside edge of the system. This lines up nicely with
SQLAlchemy’s own
<a href="https://oreil.ly/tS0E0">recommendations</a>:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>Keep the life cycle of the session (and usually the transaction) separate and
external. The most comprehensive approach, recommended for more substantial
applications, will try to keep the details of session, transaction, and
exception management as far as possible from the details of the program doing
its work.</p>
</div>
</blockquote>
<div class="attribution">
— SQLALchemy "Session Basics" Documentation
</div>
</div>
</div>
</div>
</div>
</div>
<div id="footnotes">
<hr>
<div class="footnote" id="_footnotedef_1">
<a href="#_footnoteref_1">1</a>. You may have come across the use of the word <em>collaborators</em> to describe objects that work together to achieve a goal. The unit of work and the repository are a great example of collaborators in the object-modeling sense. In responsibility-driven design, clusters of objects that collaborate in their roles are called <em>object neighborhoods</em>, which is, in our professional opinion, totally adorable.
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2020-03-10 17:15:49 UTC
</div>
</div>
<style>
pre.pygments .hll { background-color: #ffffcc }
pre.pygments { background: #f0f0f0; }
pre.pygments .tok-c { color: #60a0b0; font-style: italic } /* Comment */
pre.pygments .tok-err { border: 1px solid #FF0000 } /* Error */
pre.pygments .tok-k { color: #007020; font-weight: bold } /* Keyword */
pre.pygments .tok-o { color: #666666 } /* Operator */
pre.pygments .tok-ch { color: #60a0b0; font-style: italic } /* Comment.Hashbang */
pre.pygments .tok-cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */
pre.pygments .tok-cp { color: #007020 } /* Comment.Preproc */
pre.pygments .tok-cpf { color: #60a0b0; font-style: italic } /* Comment.PreprocFile */
pre.pygments .tok-c1 { color: #60a0b0; font-style: italic } /* Comment.Single */
pre.pygments .tok-cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */
pre.pygments .tok-gd { color: #A00000 } /* Generic.Deleted */
pre.pygments .tok-ge { font-style: italic } /* Generic.Emph */
pre.pygments .tok-gr { color: #FF0000 } /* Generic.Error */