131
131
< h2 id ="appendix_django "> Appendix D: Repository and Unit of Work Patterns with Django</ h2 >
132
132
< div class ="sectionbody ">
133
133
< div class ="paragraph ">
134
- < p > Suppose you wanted to use Django instead of SQLAlchemy and Flask. How
134
+ < p >
135
+
136
+ Suppose you wanted to use Django instead of SQLAlchemy and Flask. How
135
137
might things look? The first thing is to choose where to install it. We put it in a separate
136
138
package next to our main allocation code:</ p >
137
139
</ div >
@@ -197,7 +199,10 @@ <h2 id="appendix_django">Appendix D: Repository and Unit of Work Patterns with D
197
199
< div class ="sect2 ">
198
200
< h3 id ="_repository_pattern_with_django "> Repository Pattern with Django</ h3 >
199
201
< div class ="paragraph ">
200
- < p > We used a plug-in called
202
+ < p >
203
+
204
+
205
+ We used a plugin called
201
206
< a href ="https://github.com/pytest-dev/pytest-django "> < code > pytest-django</ code > </ a > to help with test
202
207
database management.</ p >
203
208
</ div >
@@ -299,7 +304,9 @@ <h3 id="_repository_pattern_with_django">Repository Pattern with Django</h3>
299
304
< div class ="sect3 ">
300
305
< h4 id ="_custom_methods_on_django_orm_classes_to_translate_tofrom_our_domain_model "> Custom Methods on Django ORM Classes to Translate to/from Our Domain Model</ h4 >
301
306
< div class ="paragraph ">
302
- < p > Those custom methods look something like this:</ p >
307
+ < p >
308
+
309
+ Those custom methods look something like this:</ p >
303
310
</ div >
304
311
< div id ="django_models " class ="exampleblock ">
305
312
< div class ="title "> Django ORM with custom methods for domain model conversion (src/djangoproject/alloc/models.py)</ div >
@@ -372,6 +379,8 @@ <h4 id="_custom_methods_on_django_orm_classes_to_translate_tofrom_our_domain_mod
372
379
< td class ="content ">
373
380
As in < a href ="/book/chapter_02_repository.html "> [chapter_02_repository]</ a > , we use dependency inversion.
374
381
The ORM (Django) depends on the model and not the other way around.
382
+
383
+
375
384
</ td >
376
385
</ tr >
377
386
</ table >
@@ -381,7 +390,9 @@ <h4 id="_custom_methods_on_django_orm_classes_to_translate_tofrom_our_domain_mod
381
390
< div class ="sect2 ">
382
391
< h3 id ="_unit_of_work_pattern_with_django "> Unit of Work Pattern with Django</ h3 >
383
392
< div class ="paragraph ">
384
- < p > The tests don’t change too much:</ p >
393
+ < p >
394
+
395
+ The tests don’t change too much:</ p >
385
396
</ div >
386
397
< div id ="test_uow_django " class ="exampleblock ">
387
398
< div class ="title "> Adapted UoW tests (tests/integration/test_uow.py)</ div >
@@ -483,15 +494,21 @@ <h3 id="_unit_of_work_pattern_with_django">Unit of Work Pattern with Django</h3>
483
494
instrumenting the domain model instances themselves, the
484
495
< code > commit()</ code > command needs to explicitly go through all the
485
496
objects that have been touched by every repository and manually
486
- update them back to the ORM.</ p >
497
+ update them back to the ORM.
498
+
499
+ </ p >
487
500
</ li >
488
501
</ ol >
489
502
</ div >
490
503
</ div >
491
504
< div class ="sect2 ">
492
505
< h3 id ="_api_django_views_are_adapters "> API: Django Views Are Adapters</ h3 >
493
506
< div class ="paragraph ">
494
- < p > The Django < em > views.py</ em > file ends up being almost identical to the
507
+ < p >
508
+
509
+
510
+
511
+ The Django < em > views.py</ em > file ends up being almost identical to the
495
512
old < em > flask_app.py</ em > , because our architecture means it’s a very
496
513
thin wrapper around our service layer (which didn’t change at all, by the way):</ p >
497
514
</ div >
@@ -537,7 +554,8 @@ <h3 id="_api_django_views_are_adapters">API: Django Views Are Adapters</h3>
537
554
< div class ="sect2 ">
538
555
< h3 id ="_why_was_this_all_so_hard "> Why Was This All So Hard?</ h3 >
539
556
< div class ="paragraph ">
540
- < p > OK, it works, but it does feel like more effort than Flask/SQLAlchemy. Why is
557
+ < p >
558
+ OK, it works, but it does feel like more effort than Flask/SQLAlchemy. Why is
541
559
that?</ p >
542
560
</ div >
543
561
< div class ="paragraph ">
@@ -549,7 +567,8 @@ <h3 id="_why_was_this_all_so_hard">Why Was This All So Hard?</h3>
549
567
high).</ p >
550
568
</ div >
551
569
< div class ="paragraph ">
552
- < p > Because Django is so tightly coupled to the database, you have to use helpers
570
+ < p >
571
+ Because Django is so tightly coupled to the database, you have to use helpers
553
572
like < code > pytest-django</ code > and think carefully about test databases, right from
554
573
the very first line of code, in a way that we didn’t have to when we started
555
574
out with our pure domain model.</ p >
@@ -570,7 +589,8 @@ <h3 id="_why_was_this_all_so_hard">Why Was This All So Hard?</h3>
570
589
< div class ="sect2 ">
571
590
< h3 id ="_what_to_do_if_you_already_have_django "> What to Do If You Already Have Django</ h3 >
572
591
< div class ="paragraph ">
573
- < p > So what should you do if you want to apply some of the patterns in this book
592
+ < p >
593
+ So what should you do if you want to apply some of the patterns in this book
574
594
to a Django app? We’d say the following:</ p >
575
595
</ div >
576
596
< div class ="ulist ">
@@ -610,7 +630,8 @@ <h3 id="_what_to_do_if_you_already_have_django">What to Do If You Already Have D
610
630
< div class ="sect2 ">
611
631
< h3 id ="_steps_along_the_way "> Steps Along the Way</ h3 >
612
632
< div class ="paragraph ">
613
- < p > Suppose you’re working on a Django project that you’re not sure is going
633
+ < p >
634
+ Suppose you’re working on a Django project that you’re not sure is going
614
635
to get complex enough to warrant the patterns we recommend, but you still
615
636
want to put a few steps in place to make your life easier, both in the medium
616
637
term and if you want to migrate to some of our patterns later. Consider the following:</ p >
@@ -657,7 +678,8 @@ <h3 id="_steps_along_the_way">Steps Along the Way</h3>
657
678
</ table >
658
679
</ div >
659
680
< div class ="paragraph ">
660
- < p > For more thoughts and actual lived experience dealing with existing
681
+ < p >
682
+ For more thoughts and actual lived experience dealing with existing
661
683
applications, refer to the < a href ="/book/epilogue_1_how_to_get_there_from_here.html "> epilogue</ a > .</ p >
662
684
</ div >
663
685
</ div >
@@ -680,7 +702,7 @@ <h3 id="_steps_along_the_way">Steps Along the Way</h3>
680
702
</ div >
681
703
< div id ="footer ">
682
704
< div id ="footer-text ">
683
- Last updated 2020-03-17 09:07:44 UTC
705
+ Last updated 2020-03-20 13:47:19 UTC
684
706
</ div >
685
707
</ div >
686
708
< style >
0 commit comments