-
Notifications
You must be signed in to change notification settings - Fork 1
/
hey_boss.html
2116 lines (1929 loc) · 91.6 KB
/
hey_boss.html
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Hey Boss, Event Sourcing Can Fix That!</title>
<meta name="description"
content="Our application has so much going on! Slow page loads and a large legacy codebase make developers feel stuck between a rock and a hard place. In this session, we will explore a strategy to add a modern codebase alongside the legacy code, allowing them to run simultaneously until you replace it. We will discuss common legacy problems like complex database queries causing slow downs and large reports that take so long to load, users must wait instead of analyzing the data. As our system has grown, we may need to separate our application into distinct parts and pass relevant information between them. Or maybe we have two distinct codebases already and each codebase is affected by the other's changes in ways we didn't expect. We will examine how to add Event Sourcing and CQRS to these pain points and throw away that legacy spaghetti!">
<meta name="author" content="Emily Stamey">
<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, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/white.css">
<link rel="stylesheet" href="css/my-style.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="./node_modules/highlight.js/styles/zenburn.css">
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<!--<link rel="stylesheet" href="plugin/accessibility/helper.css">-->
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class='footer'>
@elstamey <!--a href="https://joind.in/talk/3df64 ">https://joind.in/talk/3df64</a-->
</div>
<div class="slides">
<section>
<myColBox>
<article>
<img src="img/hey_boss/hey-boss-title.png">
</article>
</myColBox>
</section>
<section>
<myRowBox>
<article>
<myColBox>
<article>
<myRowBox>
<article>
<myColBox>
<article>
<img src="img/me.png" alt="developer">
</article>
<article>
<img src="img/Map_of_USA_NC.svg">
</article>
<article>
<img src="img/WWCode_Logo_Raleigh_Durham.png">
</article>
<article>
<img src="img/trianglePHP_sm.jpg">
</article>
</myColBox>
</article>
<article>
<myColBox>
<article>
<img src="img/php-logo.png">
</article>
<article>
<img src="img/iatf-federation-logo-colour-rgb.png">
</article>
<article>
<img src="img/mandala_madness.jpg">
</article>
</myColBox>
</article>
</myRowBox>
</article>
</myColBox>
</article>
</myRowBox>
<aside class="notes">
<ul>
<li>Supporting women in their careers in tech</li>
<li>Women leave tech at a 45% higher rate than men.</li>
<li>I'm a PHP developer. I love seeing talks about legacy strategies because I feel like most of us
support large applications and have fewer opportunities to build a new app from scratch.</li>
</ul>
</aside>
</section>
<section>
<myTitleBox><h3>I LOVE Legacy!</h3></myTitleBox>
<myColBox>
<article>
<img src="img/boots/knife_fork.jpg" alt="Fork Knife">
</article>
</myColBox>
<aside class="notes">
<p>I love to give and hear talks about Legacy solutions because there is so much creativity and problem solving! </p>
<p>Event Sourcing, I feel, is a great for fixing so many things that Legacy Applications ArE or HAVE BECOME</p>
<p>I really liked Eric Evans photo and reverence for the legacy application through the photo of the tree being propped up</p>
<p>One of my favorite photos to represent Legacy is also this one. Because so many of the legacy apps I've worked on have started out as a knife and over time our users or developers decided they needed a fork, and this is what they came up with. </p>
<p>At first when you look at it, things may be messy, but in reality this is pretty magnificent ingenuity, right?</p>
</aside>
</section>
<section>
<myTitleBox>
<h3 id="we-re-going-to-talk-about-">Learning Objectives</h3>
</myTitleBox>
<myColBox>
<article>
<ul>
<li>Basics of Event Sourcing using an example of a library (the kind where you check out books)</li>
<p> </p>
<li>Three projects I have worked on and the ways we used Event Sourcing on them</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h2 id="this-will-seem-like-overkill-for-a-little-bit">Before we begin</h2>
</myTitleBox>
<myColBox>
<article>
<p>Learning Event-Sourcing (or DDD) is tough! i</p>
<p>Be kind to learners (including yourself)</p>
<p>Event-Sourcing isn't for your whole application</p>
</article>
<!--article>
<p><img src="img/library/look_here.gif"></p>
</article-->
</myColBox>
<aside class="notes">
<li>Coming from a CRUD environment, Event-sourcing seems weird, it isn't normal to learn it all at once. It can take a while!</li>
<li>Reminder: As with all new things, be patient with yourself</li>
<li>ES isn't for all the things!</li>
<li>If we were simply managing inventory or building a blog app, we probably wouldn't need ES</li>
<li>ES is helpful for the things that make our applications difficult to maintain over time, the changes in process</li>
<li>ES is great for proving how you got to your results</li>
<li>ES is powerful in that it's flexible to changes and keeping an audit log</li>
</aside>
</section>
<section>
<myColBox>
<article>
<h1 id="event-sourcing">Event Sourcing</h1>
<p>The fundamental idea of Event Sourcing is that of ensuring <strong>every change to the state</strong> of an application <strong>is captured in an event object</strong>, and that these event objects are themselves stored in the sequence they were applied for the same lifetime as the application state itself.</p></p>
<p> Martin Fowler</p>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3>Events and Listeners</h3>
</myTitleBox>
<myColBox>
<article>
<img src="img/library/events_listeners.png" alt="An Event represented as a diamond and a Listener represented as an ear">
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3 id="an-event">An Event</h3>
</myTitleBox>
<myColBox>
<article>
<p>What happened?<br>
<strong>BookWasCheckedOut</strong></p>
<p>What do I need to remember about it?<br>
<strong>(book, patron, date)</strong></p>
</article>
<article>
<img src="img/library/lending%20desk1.png">
</article>
</myColBox>
</section>
<section>
<section>
<myTitleBox>
<h3 id="event-attributes">Event Attributes</h3>
</myTitleBox>
<myColBox>
<article>
<ul>
<li>Save only what you need to preserve</li>
<li>The rest can be looked up</li>
</ul>
<p><strong>(book id, patron id, date)</strong></p>
</article>
<article>
<img src="img/hey_boss/event-with-attributes.png">
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="event-class">Event Class</h1>
<pre><code>
<?php
namespace Library\Events;
use Library\Support\Event;
class BookWasCheckedOut
{
/**
* @var DateTime
*/
protected $checkoutDate;
/**
* @var int
*/
protected $patronId;
/**
* @var int
*/
protected $bookId;
public function __construct(DateTime $checkoutDate, PatronId $patronId, BookId $bookId)
{
$this->checkoutDate = $checkoutDate;
$this->patronId = $patronId;
$this->bookId = $bookId;
}
/**
* @return array
*/
public function serialize()
{
return [
'checkout_date' => $this->$checkoutDate->toString(),
'patron_id' => $this->patronId->toNative(),
'book_id' => $this->bookId->toNative(),
];
}
/**
* @param array $data
*
* @return static
*/
public static function deserialize($data)
{
return new BookWasCheckedOut(
DateTime::createFromFormat('j-M-Y', $data['checkout_date']),
PatronId::fromNative($data['patron_id']),
BookId::fromNative($data['book_id'])
);
}
/**
* @return int
*/
public function getBookId()
{
return $this->bookId;
}
/**
* @return DateTime
*/
public function getCheckoutDate(): DateTime
{
return $this->checkoutDate;
}
/**
* @return int
*/
public function getPatronId(): int
{
return $this->patronId;
}
}
</code></pre>
</article>
</myColBox>
<span class="fragment current-only" data-code-focus="8"></span>
<span class="fragment current-only" data-code-focus="14,19"></span>
<span class="fragment current-only" data-code-focus="26"></span>
</section>
</section>
<section>
<myTitleBox>
<h3 id="rules-to-follow">Rules for Events</h3>
</myTitleBox>
<myColBox>
<article>
<ul>
<li>Usually named as past-tense verbs</li>
<li><strong>RARELY</strong> changed</li>
<li><strong>Never</strong> deleted</li>
<li>Has attributes that are values<ul>
<li>not model, object, collection, or aggregate root</li>
</ul>
</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<img src="img/status_change/dont_delete_events.png" alt="3 symbols representing events of a deposit, a correction, and a deposit">
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3 id="don-t-store-objects">Don't store objects</h3>
</myTitleBox>
<myColBox>
<article>
<img src="img/status_change/Objects.png" alt="objects can change">
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3>If We Stored Objects in an Event...</h3>
</myTitleBox>
<myColBox>
<article>
<img src="img/status_change/EventWithObject.png" alt="Event With Object">
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<!--<h3 id="events-are-not-bionic">Events are not Bionic</h3>-->
</myTitleBox>
<myColBox>
<article>
<img src="img/hey_boss/could-should.jpg" alt="Jeff Goldblum in Jurassic Park saying you never stopped to think whether you should">
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3 id="events-rarely-change">Events rarely change</h3>
</myTitleBox>
<myColBox>
<article>
<ul>
<li>The part of the code that will change is most likely the <strong>result that follows that event</strong>.</li>
<li>The <strong>structure of the resulting data</strong> is more likely to change than the thing that happened</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<p><img src="img/library/event_with_listener.png" alt="One event with one listener handling the event"></p>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<p><img src="img/status_change/events_replaced.png" alt="One event with two listeners handling the event differently, one is thrown away"></p>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<p><img src="img/status_change/events_treated_differently.png" alt="One event with two listeners handling the event differently"></p>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="why-events-">Reasons to use Events</h1>
<ul>
<li>State transitions are important</li>
<li>We need an audit log, proof of the state we are currently in</li>
<li>The history of what happened is more important than the current state</li>
<li>Events are replayable if behavior in your application changes</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<p><img src="img/status_change/DomainMessage.png" alt="Domain Message"></p>
</article>
</myColBox>
<aside class="notes">
<li>When we talked about the definition of event sourcing, we said that we would store events in the order they happened</li>
<li>And we would always replay them in that order</li>
<li>We need to capture that order, the timestamp, and the type of event they are</li>
<li>And I like to separate this information from the Event class itself, as a sort of envelope</li>
<li>With the Event being stored as the payload.</li>
<li>This also makes it possible to store all of your events in the same eventstore</li>
</aside>
</section>
<section>
<myTitleBox>
<h3 id="event-store">Event Store</h3>
</myTitleBox>
<myColBox>
<article>
<ul>
<li>Domain-specific database</li>
<li>Based on a Publish-Subscribe message pattern</li>
</ul>
<p><img src="img/hey_boss/event-store-domain-message.png" alt="EventStore"></p>
</article>
</myColBox>
</section>
<section>
<section data-background="img/status_change/projector.png">
<aside class="notes">
<p>A set of event handlers that work together to build and maintain a table to be accessed by the read model.</p>
</aside>
</section>
<section>
<myColBox>
<article>
<h1 id="projector">Projector</h1>
<pre><code><?php
namespace Library\ReadModel;
use Library\Events\BookWasCheckedIn;
use Library\Events\BookWasCheckedOut;
use Library\Events\BookAddedToBookshelf;
use App\Support\ReadModel\Replayable;
use App\Support\ReadModel\SimpleProjector;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Connection;
class BookshelfProjector extends SimpleProjector implements Replayable
{
/**
* @var Connection
*/
private $connection;
/**
* @var string table we're playing events into
*/
private $table = 'proj_bookshelf';
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function beforeReplay()
{
$builder = $this->connection->getSchemaBuilder();
$builder->dropIfExists('proj_bookshelf');
$builder->create('proj_bookshelf_tmp', function (Blueprint $schema) {
$schema->string('book_id');
$schema->string('book_title');
$schema->string('book_author');
$schema->string('status');
$schema->string('checkout_date');
$schema->string('patron_id');
$schema->primary('book_id');
});
$this->table = 'proj_bookshelf_tmp';
}
public function afterReplay()
{
$builder = $this->connection->getSchemaBuilder();
$builder->dropIfExists('proj_bookshelf');
$builder->rename('proj_bookshelf_tmp', 'proj_bookshelf');
$this->table = 'proj_bookshelf';
}
/**
* @param BookWasCheckedOut $event
*/
public function applyBookWasCheckedOut(BookWasCheckedOut $event)
{
$bookshelfItem = BookshelfItem::where('id', $event->bookId);
$book->status = 'Checked Out';
$book->checkout_date = $event->checkoutDate;
$book->patron_id = $event->patronId;
$book->save();
}
/**
* @param BookAddedToBookshelf $event
*/
public function applyBookAddedToBookshelf(BookAddedToBookshelf $event)
{
$bookshelfItem = new BookshelfItem();
$bookshelfItem->setTable($this->table);
$bookshelfItem->bookId = $event->bookId;
$bookshelfItem->bookTitle = $event->bookTitle;
$bookshelfItem->bookAuthor = $event->bookAuthor;
$bookshelfItem->status = 'on shelf';
$bookshelfItem->save();
}
/**
* @param BookWasReturned $event
*/
public function applyBookWasReturned(BookWasReturned $event)
{
$bookshelfItem = BookshelfItem::where('id', $event->bookId);
$book->status = 'Available';
$book->checkout_date = '';
$book->patron_id = '';
$book->save();
}
}
</code></pre>
<p>A set of event handlers that work together to build and maintain a table to be accessed by the read model.</p>
</article>
</myColBox>
<span class="fragment current-only" data-code-focus="13"></span>
<span class="fragment current-only" data-code-focus="24"></span>
<span class="fragment current-only" data-code-focus="63"></span>
<span class="fragment current-only" data-code-focus="77"></span>
<span class="fragment current-only" data-code-focus="94"></span>
</section>
</section>
<section>
<section>
<myTitleBox>
<h1 id="read-model">Read Model</h1>
</myTitleBox>
<myColBox>
<article>
<img src="img/library/read_model.png" alt="Read Model table with highlighted rows and queries">
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="read-model">Read Model</h1>
<pre><code><?php
namespace Library\ReadModel;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* @codeCoverageIgnore
*/
class Bookshelf extends Model
{
protected $table = 'proj_bookshelf';
public $incrementing = false;
public $timestamps = false;
public static function lookupLoansFor($patronId)
{
return static::where('patron_id', $patronId)->get();
}
public function lookupAvailableBooks()
{
return static::where('status', 'Available')->get();
}
public function lookupOverdueBooks()
{
return static::where('checkout_date', '<', date('Y-m-d', strtotime('-7 days')))->get();
}
}
</code></pre>
</article>
</myColBox>
<span class="fragment current-only" data-code-focus="12"></span>
<span class="fragment current-only" data-code-focus="14"></span>
<span class="fragment current-only" data-code-focus="18"></span>
<span class="fragment current-only" data-code-focus="23"></span>
<span class="fragment current-only" data-code-focus="28"></span>
</section>
</section>
<section>
<myTitleBox>
<h3>Creating the events</h3>
</myTitleBox>
<myRowBox>
<article>
<ul>
<li><b>An event is created only after validation</b>
<ul>
<li>Directly in a controller 'checkout' method</li>
<li>Using a Check Out Book Command and Handler</li>
</ul>
</li>
</ul>
</article>
</myRowBox>
</section>
<section data-background="img/hey_boss/interactionToEventDiagram.jpg">
<!--<myColBox>-->
<!--<article>-->
<!--<p><img src="" alt="Diagram of the interaction on the web page then routed to the controller action, which calls a command, If valid, the Command creates the Event"></p>-->
<!--</article>-->
<!--</myColBox>-->
</section>
<!--<section data-background="img/library/bookCheckoutForm.png">-->
<!--</section>-->
<!--<section>-->
<!--<myColBox>-->
<!--<article>-->
<!--<h1 id="bookscontroller-update">CRUD: update method</h1>-->
<!--<pre><code>public function update(Request $request, $id)-->
<!--{-->
<!--// our default update method-->
<!--// validate inputs-->
<!--$Book = Book::findOrFail($id);-->
<!--$Book->update($request->all());-->
<!--return $Book;-->
<!--}-->
<!--</code></pre>-->
<!--</article>-->
<!--</myColBox>-->
<!--</section>-->
<!--<section>-->
<!--<myTitleBox>-->
<!--<h1 id="crud-to-cqrs">CRUD Checkout</h1>-->
<!--</myTitleBox>-->
<!--<myColBox>-->
<!--<article>-->
<!--<p> </p>-->
<!--<pre><code>-->
<!--public function checkOutBook(Request $request, $id)-->
<!--{-->
<!--// altered the update method-->
<!--// validate inputs-->
<!--$Book = Book::findOrFail($id);-->
<!--$Book->update('status' => 'checked out',-->
<!--'patron' => $request->patronId);-->
<!--return $Book;-->
<!--}-->
<!--</code></pre>-->
<!--<span class="fragment current-only" data-code-focus="2"></span>-->
<!--<span class="fragment current-only" data-code-focus="7-8"></span>-->
<!--</article>-->
<!--<article>-->
<!--<p> </p>-->
<!--<p class="fragment"><img src="img/library/itDoesntFit.gif" alt="cat climbing into a trash pail but doesn't quite fit"></p>-->
<!--</article>-->
<!--</myColBox>-->
<!--</section>-->
<!--<section>-->
<!--<myTitleBox>-->
<!--<h1 id="crud-to-cqrs">CRUD Checkout</h1>-->
<!--</myTitleBox>-->
<!--<myColBox>-->
<!--<article>-->
<!--<p> </p>-->
<!--<pre><code>-->
<!--public function checkOutBook(Request $request, $id)-->
<!--{-->
<!--// altered the update method-->
<!--// validate book can be checked out-->
<!--$event = new BookWasCheckedOut($request->bookId,-->
<!--$request->patronId,-->
<!--time());-->
<!--}-->
<!--</code></pre>-->
<!--<span class="fragment current-only" data-code-focus="5"></span>-->
<!--<span class="fragment current-only" data-code-focus="7-9"></span>-->
<!--</article>-->
<!--</myColBox>-->
<!--</section>-->
<section>
<myRowBox>
<myTitleBox>
<h1>CQRS</h1>
</myTitleBox>
<article>
<br>
<h3>Command and Query Response Segregation</h3>
<ul>
<p>An application architecture pattern commonly used with event sourcing</p>
<p>CQRS involves splitting an application into two parts internally.</p>
</ul>
</article>
</myRowBox>
</section>
<section>
<myRowBox>
<article>
<h1 id="cqrs">CQRS</h1>
<ul>
<li><strong>Command</strong> is any method that mutates state</li>
<li><strong>Query</strong> is any method that returns a value</li>
<li>Should only be used on specific portions of a system, not the system as a whole</li>
</ul>
</article>
</myRowBox>
</section>
<!--<section>-->
<!--<myColBox>-->
<!--<article>-->
<!--<h1 id="command-handler">Command Handler</h1>-->
<!--<p>A command handler receives a command and brokers a result from the appropriate aggregate. "A result" is either a successful application of the command, or an exception.</p>-->
<!--<p><strong> should affect one and only one aggregate </strong></p>-->
<!--</article>-->
<!--</myColBox>-->
<!--</section>-->
<section>
<section>
<myColBox>
<article>
<h1 id="command-handler">Command Handler</h1>
<ol>
<li>Validate the command on its own merits.</li>
<li>Validate the command on the current state of the aggregate.</li>
<li>If validation is successful, create an event(s)</li>
<li>Attempt to persist the new events. If there's a concurrency conflict during this step, retry or exit.</li>
</ol>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3 id="crud-to-cqrs">Command Handler example</h3>
</myTitleBox>
<myColBox>
<article>
<pre><code>public function update(Request $request)
{
// $request has book id, patron id
try {
$command = new CheckOutBook($request->bookId, $request->patronId);
$this->bookLendingService->handleCheckOutBook($command);
} catch (InvalidUserException $e) {
return response()->json("Not authorized to check out a book.", Response::HTTP_FORBIDDEN);
} catch (BookUnavailableException $e) {
return response()->json("Book was not available to be checked out", 400);
}
<------ dynamic command handler ------>
private function handle(Command $command)
{
$method = $this->getHandleMethod($command);
if (! method_exists($this, $method)) {
return;
}
$this->$method($command);
}
private function getHandleMethod(Command $command)
{
return 'handle' . class_basename($command);
}
<-------- handle check out book command --------->
public function handleCheckoutOutBook(CheckOutBook $command)
{
$book = Book::findOrFail($command->bookId);
$patron = Patron::findOrFail($command->patronId);
if (!$book->isAvailable()) {
throw new BookUnavailableException();
}
if (!$patron->isAuthorized()) {
throw new InvalidUserException();
}
//record the event
$this->record(
new BookWasCheckedOut(date("Y-m-d H:i:s"),
$patron->getId(),
$book->getId())
);
}
</code></pre>
<span class="fragment current-only" data-code-focus="1"></span>
<span class="fragment current-only" data-code-focus="7"></span>
<span class="fragment current-only" data-code-focus="9"></span>
<span class="fragment current-only" data-code-focus="11-14"></span>
<span class="fragment current-only" data-code-focus="38"></span>
<span class="fragment current-only" data-code-focus="40-41"></span>
<span class="fragment current-only" data-code-focus="43-49"></span>
<span class="fragment current-only" data-code-focus="52-56"></span>
</article>
</myColBox>
</section>
</section>
<section>
<myColBox>
<article>
<h1 id="separate-read-write">Optimize</h1>
<ul>
<li>Separate the load from reads and writes allowing you to scale each independently. </li>
<ul>
<li>All <strong>commands</strong> go into a WriteService</li>
<li>All <strong>queries</strong> go into a ReadService</li>
</ul></li>
</ul>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3>You can choose which is best</h3>
</myTitleBox>
<myColBox>
<article>
<ul>
<li>CRUD</li>
<li>Event-Sourcing</li>
<li>Event-Sourcing with CQRS</li>
<li>Event-Sourcing, CQRS, DDD</li>
</ul>
</article>
<article>
<img src="img/hey_boss/captain-planet-power.jpg">
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3>My Projects</h3>
</myTitleBox>
<myColBox>
<article>
<ul>
<li>Scholarships</li>
<li>Course Registration</li>
<li>Threat Reports</li>
</ul>
</article>
</myColBox>
</section>
<section data-background-image="img/hey_boss/bg-scholarships.png">
<myColBox>
<article>
<img src="img/boots/scholarships_diagram.png" alt="Scholarships Project">
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<img src="img/boots/bounded_contexts.png" alt="bounded contexts">
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<img src="img/hey_boss/scholarships-bounded-contexts-events.png" alt="bounded contexts with Events">
</article>
</myColBox>
</section>
<!--<section>-->
<!--<myRowBox>-->
<!--<article>-->
<!--<myColBox>-->
<!--<article>-->
<!--<img src="img/boots/octocat_Professortocat_v2.png" alt="github">-->
<!--</article>-->
<!--<article>-->
<!--<img src="img/boots/vagrant-virtualbox.png" alt="virtualization">-->
<!--</article>-->
<!--<article>-->
<!--<img src="img/boots/phpunit.png" alt="tests">-->
<!--</article>-->
<!--</myColBox>-->
<!--</article>-->
<!--<article>-->
<!--<myColBox>-->
<!--<article>-->
<!--<img src="img/boots/logo-composer-transparent.png" alt="composer">-->
<!--</article>-->
<!--<article>-->
<!--<img src="img/hey_boss/twig.jpg" alt="twig">-->
<!--</article>-->
<!--<article>-->
<!--<img src="img/hey_boss/pimple.jpg" alt="pimple">-->
<!--</article>-->
<!--</myColBox>-->
<!--</article>-->
<!--</myRowBox>-->
<!--</section>-->
<!--<section>-->
<!--<myColBox>-->
<!--<article>-->
<!--<img src="img/hey_boss/scholarships-filetree-legacy.png" alt="filetree old only">-->
<!--</article>-->
<!--</myColBox>-->
<!--</section>-->
<!--<section>-->
<!--<myColBox>-->
<!--<article>-->
<!--<img src="img/hey_boss/scholarships-filetree.png" alt="filetree with new code">-->
<!--</article>-->
<!--</myColBox>-->
<!--</section>-->
<!--<section>-->
<!--<myTitleBox>-->
<!--<h3>Bootstrapping: Namespaces</h3>-->
<!--</myTitleBox>-->
<!--<myColBox>-->
<!--<article>-->
<!--<ul>-->
<!--<li>'/src' is given a namespace</li>-->
<!--<li>namespaces are autoloaded in composer</li>-->
<!--</ul>-->
<!--<span class="fragment current-only" data-code-focus="7-11"></span>-->
<!--</article>-->
<!--</myColBox>-->
<!--<footer>-->
<!--<pre><code>-->
<!--{-->
<!--"name": "itecs/scholarships",-->
<!--"description": "Scholarships application for the College of Engineering.",-->
<!--...-->
<!--"autoload": {-->
<!--"psr-4": {-->
<!--"ITECS\\Scholarships\\": [ "src/", "app/core/" ],-->
<!--"Codeception\\Module\\": "src/",-->
<!--"Tests\\Substitute\\": "tests/_helpers/"-->
<!--}-->
<!--}-->
<!--}-->
<!--</code></pre>-->
<!--</footer>-->