-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary.html
1197 lines (1042 loc) · 36.8 KB
/
library.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>Event Sourcing a Small Library</title>
<meta name="description"
content="Whether you want to keep track of a tiny library or your friends borrowing from your bookshelf, event sourcing can help you collect where your books are going. When you are ready, the simple check in and check out events can build reports of what books need to be returned, or where a book may have traveled. We will use this simple project to navigate the data structures in event sourcing and show how you may use event sourcing in your next project.">
<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
</div>
<div class="slides">
<section data-background=" class="titleblock">
<myTitleBox><h2 id="event-sourcing-a-small-library">Event Sourcing a Small Library</h2></myTitleBox>
<myColBox>
<article>
<p> Emily Stamey <br> @elstamey</p>
</article>
<article>
<img src="img/library/IMG_20180129_201035.jpg">
</article>
</myColBox>
</section>
<section>
<myRowBox>
<article>
<myColBox>
<article>
<img src="img/me.png" alt="developer">
</article>
<article>
<li>PHP developer</li>
<li>Organizer of Triangle PHP </li>
<li>Director WWCode Raleigh-Durham</li>
<li>Work at InQuest</li>
<p> </p>
<myColBox>
<article>
<img src="img/WWCode_Logo_Raleigh_Durham.png">
</article>
<article>
<img src="img/trianglePHP_sm.jpg">
</article>
<article>
<img src="img/inquest.svg">
</article>
</myColBox>
</article>
</myColBox>
</article>
</myRowBox>
</section>
<section data-background-image="img/family.png" class="titleblock">
</section>
<section>
<myColBox>
<article>
<p><img src="img/library/ideaGuy.png" alt="My kid and all of his ideas"></p>
</article>
</myColBox>
</section>
<section data-background-image="img/library/TheLibrarian.jpg" class="titleblock"
<h3 id="the-librarian">The Librarian</h3>
</section>
<section>
<myRowBox>
<article>
<img src="img/library/DarthVaderIsMyPM.jpg" alt="the library">
</article>
</myRowBox>
</section>
<section>
<myTitleBox>
<h1 id="requirements">Requirements</h1>
</myTitleBox>
<myColBox>
<article>
<p> </p>
<ul>
<li>Check out books</li>
<li>Reserve books</li>
<li>Renew books</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h1 id="the-library-process">The Librarian's Process</h1>
</myTitleBox>
<myColBox>
<article>
<p> </p>
<ul>
<li>Check in and Check out Books</li>
<li>View a List of Books I Own</li>
<li>See who has checked out books</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="crud">Crud</h1>
<pre><code><?php
namespace Library\Http\Controllers\Api\V1;
use Illuminate\Http\Request;
use Library\Book;
use Library\Http\Controllers\Controller;
class BooksController extends Controller
{
public function index()
{
return Book::all();
}
public function show($id)
{
return Book::findOrFail($id);
}
public function update(Request $request, $id)
{
$Book = Book::findOrFail($id);
$Book->update($request->all());
return $Book;
}
public function store(Request $request)
{
$Book = Book::create($request->all());
return $Book;
}
public function destroy($id)
{
$Book = Book::findOrFail($id);
$Book->delete();
return '';
}
}
</code></pre>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h1 id="checking-out-and-checking-in-books">Book Check-in/out</h1>
</myTitleBox>
<myColBox>
<article>
<p> </p>
<ul>
<li>Modify update method to change status</li>
<li>Using a status table to show if the book is in or out</li>
<li>Any of this would be fine</li>
</ul>
</article>
<article>
<img src="img/library/lending%20desk1.png">
</article>
</myColBox>
</section>
<section data-background="img/status_change/CompensatingMeasures.png">
</section>
<section>
<myTitleBox>
<h2 id="this-will-seem-like-overkill-for-a-little-bit">Pause for Warning</h2>
</myTitleBox>
<myColBox>
<article>
<p>Switch from CRUD to ES is tough</p>
<p>#NotAllApplications <strong>need</strong> to be Event-Sourced</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, but this is a light-weight approach to using it</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 only checking in and out books, this would be too much</li>
<li>ES is powerful in that it's flexible to changes and keeping an audit log</li>
</aside>
</section>
<section>
<myTitleBox>
<h1 id="we-re-going-to-talk-about-">Learning Objectives:</h1>
</myTitleBox>
<myColBox>
<article>
<p> </p>
<ul>
<li>What is Event Sourcing<ul>
<li>Rules to Follow</li>
<li>Data Structures: Events, Projections, Read Models, CQRS(briefly)</li>
</ul>
</li>
<li>How our code will change </li>
<li>Why would we want to use Event Sourcing</li>
</ul>
</article>
</myColBox>
</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>
<myColBox>
<article>
<h1 id="events-and-listeners">Events and Listeners</h1>
<p><img src="img/library/events_listeners.png" alt="An Event represented as a diamond and a Listener represented as an ear"></p>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="an-event">An Event</h1>
<br>
<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>
</myColBox>
</section>
<section>
<myTitleBox>
<h1 id="event-attributes">Attributes</h1>
</myTitleBox>
<myColBox>
<article>
<p> </p>
<p>Save only what you need to preserve, The rest can be looked up</p>
<p><strong>(book id, patron id, date)</strong></p>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="rules-to-follow">Rules to Follow</h1>
<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>
<p><img src="img/status_change/dont_delete_events.png" alt="3 symbols representing events of a deposit, a correction, and a deposit"></p>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="don-t-store-objects">Don't store objects</h1>
<p><img src="img/status_change/Objects.png" alt="objects can change"></p>
</article>
</myColBox>
</section>
<section>
<myTitleBox><h3>If We Stored it in an Event...</h3></myTitleBox>
<myColBox>
<article>
<img src="img/status_change/EventWithObject.png" alt="Event With Object">
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="events-are-not-bionic">Events are not Bionic</h1>
<p><img src="img/status_change/we_can_rebuild_him.jpg" alt="Events and Bionic Cat"></p>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h2 id="events-rarely-change">Events rarely change</h2>
</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>
<h1 id="event-class">Event Class</h1>
<pre><code>
<?php
namespace Library\Events;
use Library\Support\Event;
class BookWasCheckedOut
{
/**
* @var DateTime
*/
public $checkoutDate;
/**
* @var int
*/
public $patronId;
/**
* @var int
*/
public $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'])
);
}
}
</code></pre>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h1>Connecting the events</h1>
</myTitleBox>
<myRowBox>
<article>
<p> </p>
<ul>
<li>An event is created only after validation</li>
<ul>
<li>directly in a controller 'checkout' method</li>
<li>using a Check Out Book Command and Handler</li>
</ul>
</ul>
</article>
</myRowBox>
</section>
<section>
<myColBox>
<article>
<p><img src="img/status_change/DomainMessage.png" alt="Domain Message"></p>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="event-store">Event Store</h1>
<ul>
<li>Domain-specific database</li>
<li>Based on a Publish-Subscribe message pattern</li>
</ul>
<p><img src="img/status_change/EventStore.png" alt="EventStore"></p>
</article>
</myColBox>
</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\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();
}
}
</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>
</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', 'on shelf')->get();
}
public function lookupOverdueBooks()
{
return static::where('checkout_date', '<', date('Y-m-d', strtotime('-7 days')))->get();
}
}
</code></pre>
</article>
</myColBox>
</section>
<!--<section>-->
<!--<myTitleBox>-->
<!--<h1>Aggregate Root</h1>-->
<!--</myTitleBox>-->
<!--<myRowBox>-->
<!--<article>-->
<!--<p>An aggregate root is an entity that is modeled using events. </p>-->
<!--</article>-->
<!--</myRowBox>-->
<!--</section>-->
<section data-background="img/library/interactionToEventDiagram.png">
<!--<myColBox>-->
<!--<article>-->
<!--<p><img src="" alt="Diagram of the intearction 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</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>
<article>
<h1 id="cqrs">CQRS</h1>
<p><strong>Command and Query Response Segregation</strong></p>
<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>These methods become part of Services</li>
<li>When parts of your application no longer fit a CRUD model</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="crud-to-cqrs">CRUD to CQRS</h1>
<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 request enrollment.", Response::HTTP_FORBIDDEN);
} catch (BookUnavailableException $e) {
return response()->json("Book was not available to be checked out", 400);
return $Book;
}
<------ 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="40"></span>
<span class="fragment current-only" data-code-focus="42-43"></span>
<span class="fragment current-only" data-code-focus="45-51"></span>
<span class="fragment current-only" data-code-focus="54-58"></span>
</article>
</myColBox>
</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>
<!----- ** cannot call a read side** ----->
</article>
</myColBox>
</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>
<myColBox>
<article>
<h1 id="separate-read-write">Optimize</h1>
<ul>
<li>all <strong>commands</strong> go into a WriteService</li>
<li>all <strong>queries</strong> go into a ReadService</li>
<li>to optimize your application for reads and writes</li>
<li>Separate the load from reads and writes allowing you to scale each independently. <ul>
<li><em>If your application sees a big disparity between reads and writes this is very handy.</em> </li>
</ul>
</li>
<li>Uses a separate model for all queries</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="task-based-ui">Task-based UI</h1>
<ul>
<li>Track what the user is doing and push forward commands representing the intent of the user </li>
<li>CQRS <strong>does not require</strong> a task based UI (DDD does)</li>
<li>CQRS used in a CRUD interface, makes creating separated data models harder</li>
</ul>
</article>
</myColBox>
</section>
<section data-background="img/library/library_angryKid.jpg">
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<myTitleBox>
<h1 id="library" style="background: rgba(255,255,2555,0.8);">Library</h1>
</myTitleBox>
<myTitleBox style="background: rgba(255,255,2555,0.8); margin: 1em;">
<ul>
<li>May not be a purely task-based UI</li>
<li>I don't need to optimize for load<br></li>
</ul>
</myTitleBox>
</section>
<section>
<myColBox>
<article>
<p><img src="img/library/bookCheckoutForm2.png" alt="Book Form to Request Hold"></p>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="booklendingservice">BookLendingService</h1>
<ul>
<li>CheckOutBook</li>
<li>CheckInBook</li>
<li>RequestBook</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="flexibility-gained-long-term">Flexibility gained long term</h1>
<ul>
<li>Aren't locked into the current interpretation of events</li>
<li>Can track the events and build more views of the data and add functionality later</li>
<li>Financial reports, can show budget balance on any given day in the past, prove how you got the current balance</li>
<li>Could display everyone who checked out a book, and style it like old cards in the book pockets</li>
</ul>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<p><img src="img/library/libraryCardBackPocket.jpg" alt="Book Back Pocket with names of people who checked it out"></p>
</article>
</myColBox>
</section>
<section>
<myTitleBox>
<h3 id="adding-event-sourcing-to-your-legacy-app">Adding Event Sourcing to Your Legacy App</h3>
</myTitleBox>
<myRowBox>
<article><img src="img/library/overview.png"> </article>
</myRowBox>
</section>
<section>
<myColBox>
<article>
<h1 id="resources">Resources</h1>
<ul>
<li><p>Tools</p>
<ul>
<li><a href="https://eventsauce.io/">Event Sauce - Event-sourcing & Commands</a></li>
<li><a href="http://getprooph.org/">Prooph - CQRS and ES</a></li>
<li><a href="https://github.com/broadway/broadway">Broadway - framework for CQRS and ES</a></li>
</ul>
</ul>
</article>
</myColBox>
</section>
<section>
<myColBox>
<article>
<h1 id="resources">Resources</h1>
<ul>
<li><a href="https://martinfowler.com/bliki/CQRS.html">CQRS by Martin Fowler</a></li>
<li><p><a href="http://codebetter.com/gregyoung/2010/02/16/cqrs-task-based-uis-event-sourcing-agh/">CQRS by Greg Young</a></p></li>
<li><p>videos</p>
<ul>
<li><a href="https://www.youtube.com/watch?v=JHGkaShoyNs">Greg Young CQRS and Event Sourcing</a></li>
<li><a href="https://www.youtube.com/watch?v=whCk1Q87_ZI">Greg Young - long class</a></li>
<li><a href="https://www.youtube.com/watch?v=LDW0QWie21s">Greg Young - A Decade of DDD, CQRS, Event Sourcing</a></li>
</ul>
</li>
<li><p>podcasts</p>
<ul>
<li><a href="https://www.phproundtable.com/episode/event-sourcing-in-php">PHP Round Table - Event Sourcing</a> (+2 more)</li>
</ul>
</li>
</ul>
</article>
</myColBox>