Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Event Driven
Applications
Wrangle Cross-Cutting
Concerns
Chris Saylor
Lead Engineer
@cjsaylor
150+ countries
14 million weekly class
participants
15 million monthly pageviews
47 m...
What to expect
What are cross-cutting concerns?
Origins from Observer pattern
Asynchronous vs Synchronous
Logic abstractio...
What are cross-cutting
concerns?
Cross-cutting concerns are parts of a program that
rely on or must affect many other parts of the
system.
Source: Wikipedi...
Examples of cross-cutting concerns
Logging
Caching
Product feature interaction
Monitoring
Record audit trail
State Managem...
What are the risks of cross cutting
concerns?
Coupling systems too tightly (aka "Tangling")
Lots of code duplications (aka...
Single Responsibility Principle
If a class and its services should be responsible for
one thing, how do we deal with unrel...
Origins from Observer pattern
Observer Pattern
The root of Event Driven Applications began with the observer
pattern which tracks the state of a subject...
SplObserver
The interface is a standard lib to implement this
pattern.
SplObserver
An SplSubject object attaches the obser...
Asynchronous vs Synchronous
App event in javascript
Syncronous in order of execution
Asyncronous in order of completion
$('p')
.on('click', function()...
App event in PHP
Syncronous
Predictable order of execution and completion
<?php
$event
->on('event1', function() {
echo "F...
Logic abstraction
Separation of Concerns
Use synchronous events to separate chunks of business logic away
from core functionality
Provides f...
Example Before
<?php
function login($username, $password) {
try {
// get user from db
$user = Repository::get($username, $...
Example After
<?php
function login($username, $password) {
try {
// get user from db
$user = Repository::get($username, $p...
Demo
https://github.com/cjsaylor/event-driven-apps-demo
Branch: cart-demo (cart-demo-event)
Events in the wild
CakePHP
Controllers have beforeFilter, beforeRender, and components with
similar callbacks
Models have behaviors with befo...
Symfony 2
can be used as a standalone event library.
Can be easily incorporated via Composer
EventDispatcher
Zend
can be used to create events and listeners of said
events
Can be extracted from Zend, but not as easily as Symfony 2....
Many others...
Guzzle
Magento
Lithium
Advantages and Drawbacks
Decouple Code
Advantage:
Modular design can reduce core functionality bugs when modifying
modules.
Allows for open framewo...
Plugin Architecture
Advantage:
Enable or disable plugins on the fly.
Essential for open source targeting developers to imp...
Testing
Advantage:
Test the listeners separate from the core functionality.
Execute listeners from core test cases via ext...
Further considerations
Synchronous or
Asynchronous?
Make a judgment on whether an event listener should process it now
or post-request process wi...
Events and Variable Reference
Using an object for passage into the event so all event handlers have
access to the same dat...
Specialization
Is the logic I'm trying to abstract into an event worth doing so? Would
this logic best be housed in the co...
Further Reading
Using application events to hook in plugins
Decoupling applications with domain events
Domain Events
Thank you
Get this presentation on slideshare:
http://www.slideshare.com/cjsaylor/event-driven-application
Upcoming SlideShare
Loading in …5
×

Event driven application

4,901 views

Published on

Event driven development in PHP applications are becoming more prevalent. This presentation goes over the origins of the concept from the Observer pattern, details the benefits and drawbacks, and discusses real world application of the pattern.

Published in: Technology, Education
  • DOWNLOAD FULL. BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL. BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD FULL. BOOKS INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... 1.DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/y8nn3gmc } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Event driven application

  1. 1. Event Driven Applications Wrangle Cross-Cutting Concerns
  2. 2. Chris Saylor Lead Engineer @cjsaylor 150+ countries 14 million weekly class participants 15 million monthly pageviews 47 million monthly service requests Zumba Fitness
  3. 3. What to expect What are cross-cutting concerns? Origins from Observer pattern Asynchronous vs Synchronous Logic abstraction Demo Events in the wild Advantages and drawbacks Futher considerations
  4. 4. What are cross-cutting concerns?
  5. 5. Cross-cutting concerns are parts of a program that rely on or must affect many other parts of the system. Source: Wikipedia.org
  6. 6. Examples of cross-cutting concerns Logging Caching Product feature interaction Monitoring Record audit trail State Management
  7. 7. What are the risks of cross cutting concerns? Coupling systems too tightly (aka "Tangling") Lots of code duplications (aka "Scattering")
  8. 8. Single Responsibility Principle If a class and its services should be responsible for one thing, how do we deal with unrelated business rules that must be addressed by that class?
  9. 9. Origins from Observer pattern
  10. 10. Observer Pattern The root of Event Driven Applications began with the observer pattern which tracks the state of a subject by attaching observers to the subject so that when it changes, all observers are notified.
  11. 11. SplObserver The interface is a standard lib to implement this pattern. SplObserver An SplSubject object attaches the observer. The SplSubject then calls an update on the observer when notifying the registered observers.
  12. 12. Asynchronous vs Synchronous
  13. 13. App event in javascript Syncronous in order of execution Asyncronous in order of completion $('p') .on('click', function() { (function() { console.log('first?'); }()); }) .on('click', function() { (function() { console.log('second?'); }()); });
  14. 14. App event in PHP Syncronous Predictable order of execution and completion <?php $event ->on('event1', function() { echo "First!"; }) ->on('event1', function() { echo "Second." });
  15. 15. Logic abstraction
  16. 16. Separation of Concerns Use synchronous events to separate chunks of business logic away from core functionality Provides for modularity Allow for unit testing listeners Multiple messages - multiple listeners
  17. 17. Example Before <?php function login($username, $password) { try { // get user from db $user = Repository::get($username, $password); // Check if an admin if ($user->role->admin) { $_SESSION['admin'] = true; } // Log user's login $user->updateLastLogin(time()); Repository::saveUser($user); } catch (Exception $e) { return false; } return true; }
  18. 18. Example After <?php function login($username, $password) { try { // get user from db $user = Repository::get($username, $password); // Fire post login event $event::trigger('afterLogin', compact('user')); } catch (Exception $e) { return false; } return true; <?php $event::listen('afterLogin', function($event) { if ($event->data['user']->role->admin) { $_SESSION['admin'] = true } }); $event::listen('afterLogin', function($event) { $event->data['user']->updateLastLogin(time()); Repository::saveUser($event->data['user']); });
  19. 19. Demo https://github.com/cjsaylor/event-driven-apps-demo Branch: cart-demo (cart-demo-event)
  20. 20. Events in the wild
  21. 21. CakePHP Controllers have beforeFilter, beforeRender, and components with similar callbacks Models have behaviors with before find/save Exposed event library
  22. 22. Symfony 2 can be used as a standalone event library. Can be easily incorporated via Composer EventDispatcher
  23. 23. Zend can be used to create events and listeners of said events Can be extracted from Zend, but not as easily as Symfony 2. EventManagers
  24. 24. Many others... Guzzle Magento Lithium
  25. 25. Advantages and Drawbacks
  26. 26. Decouple Code Advantage: Modular design can reduce core functionality bugs when modifying modules. Allows for open frameworks to allow third parties to implement custom solutions without modifying core files. Disadvantage: Code is harder to follow and needs good organizational management. Documentation of what events will be called when is almost a must.
  27. 27. Plugin Architecture Advantage: Enable or disable plugins on the fly. Essential for open source targeting developers to implement. Disadvantage: No dependencies should exist be between plugins.
  28. 28. Testing Advantage: Test the listeners separate from the core functionality. Execute listeners from core test cases via external event triggers. Mock event callbacks to test event trigger placement. Disadvantage: Multiple event bindings in test suites results in undesired event triggers. Unbinding specific event callbacks can be difficult to do.
  29. 29. Further considerations
  30. 30. Synchronous or Asynchronous? Make a judgment on whether an event listener should process it now or post-request process with a call to a message queue.
  31. 31. Events and Variable Reference Using an object for passage into the event so all event handlers have access to the same data.
  32. 32. Specialization Is the logic I'm trying to abstract into an event worth doing so? Would this logic best be housed in the core logic?
  33. 33. Further Reading Using application events to hook in plugins Decoupling applications with domain events Domain Events
  34. 34. Thank you Get this presentation on slideshare: http://www.slideshare.com/cjsaylor/event-driven-application

×