Slide 1

Slide 1 text

Event Driven Applications Wrangle Cross-Cutting Concerns

Slide 2

Slide 2 text

Chris Saylor Lead Engineer @cjsaylor 150+ countries 14 million weekly class participants 15 million monthly pageviews 47 million monthly service requests Zumba Fitness

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

What are cross-cutting concerns?

Slide 5

Slide 5 text

Cross-cutting concerns are parts of a program that rely on or must affect many other parts of the system. Source: Wikipedia.org

Slide 6

Slide 6 text

Examples of cross-cutting concerns Logging Caching Product feature interaction Monitoring Record audit trail State Management

Slide 7

Slide 7 text

What are the risks of cross cutting concerns? Coupling systems too tightly (aka "Tangling") Lots of code duplications (aka "Scattering")

Slide 8

Slide 8 text

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?

Slide 9

Slide 9 text

Origins from Observer pattern

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

Asynchronous vs Synchronous

Slide 13

Slide 13 text

App event in javascript Syncronous in order of execution Asyncronous in order of completion $ ( ' p ' ) . o n ( ' c l i c k ' , f u n c t i o n ( ) { ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' f i r s t ? ' ) ; } ( ) ) ; } ) . o n ( ' c l i c k ' , f u n c t i o n ( ) { ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' s e c o n d ? ' ) ; } ( ) ) ; } ) ;

Slide 14

Slide 14 text

App event in PHP Syncronous Predictable order of execution and completion < ? p h p $ e v e n t - > o n ( ' e v e n t 1 ' , f u n c t i o n ( ) { e c h o " F i r s t ! " ; } ) - > o n ( ' e v e n t 1 ' , f u n c t i o n ( ) { e c h o " S e c o n d . " } ) ;

Slide 15

Slide 15 text

Logic abstraction

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Example Before < ? p h p f u n c t i o n l o g i n ( $ u s e r n a m e , $ p a s s w o r d ) { t r y { / / g e t u s e r f r o m d b $ u s e r = R e p o s i t o r y : : g e t ( $ u s e r n a m e , $ p a s s w o r d ) ; / / C h e c k i f a n a d m i n i f ( $ u s e r - > r o l e - > a d m i n ) { $ _ S E S S I O N [ ' a d m i n ' ] = t r u e ; } / / L o g u s e r ' s l o g i n $ u s e r - > u p d a t e L a s t L o g i n ( t i m e ( ) ) ; R e p o s i t o r y : : s a v e U s e r ( $ u s e r ) ; } c a t c h ( \ E x c e p t i o n $ e ) { r e t u r n f a l s e ; } r e t u r n t r u e ; }

Slide 18

Slide 18 text

Example After < ? p h p f u n c t i o n l o g i n ( $ u s e r n a m e , $ p a s s w o r d ) { t r y { / / g e t u s e r f r o m d b $ u s e r = R e p o s i t o r y : : g e t ( $ u s e r n a m e , $ p a s s w o r d ) ; / / F i r e p o s t l o g i n e v e n t $ e v e n t : : t r i g g e r ( ' a f t e r L o g i n ' , c o m p a c t ( ' u s e r ' ) ) ; } c a t c h ( \ E x c e p t i o n $ e ) { r e t u r n f a l s e ; } r e t u r n t r u e ; < ? p h p $ e v e n t : : l i s t e n ( ' a f t e r L o g i n ' , f u n c t i o n ( $ e v e n t ) { i f ( $ e v e n t - > d a t a [ ' u s e r ' ] - > r o l e - > a d m i n ) { $ _ S E S S I O N [ ' a d m i n ' ] = t r u e } } ) ; $ e v e n t : : l i s t e n ( ' a f t e r L o g i n ' , f u n c t i o n ( $ e v e n t ) { $ e v e n t - > d a t a [ ' u s e r ' ] - > u p d a t e L a s t L o g i n ( t i m e ( ) ) ; R e p o s i t o r y : : s a v e U s e r ( $ e v e n t - > d a t a [ ' u s e r ' ] ) ;

Slide 19

Slide 19 text

Demo https://github.com/cjsaylor/event-driven-apps-demo Branch: cart-demo (cart-demo-event)

Slide 20

Slide 20 text

Events in the wild

Slide 21

Slide 21 text

CakePHP Controllers have beforeFilter, beforeRender, and components with similar callbacks Models have behaviors with before find/save Exposed event library

Slide 22

Slide 22 text

Symfony 2 can be used as a standalone event library. Can be easily incorporated via Composer EventDispatcher

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Many others... Guzzle Magento Lithium

Slide 25

Slide 25 text

Advantages and Drawbacks

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

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.

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

Further considerations

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

Events and Variable Reference Using an object for passage into the event so all event handlers have access to the same data.

Slide 32

Slide 32 text

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?

Slide 33

Slide 33 text

Further Reading Using application events to hook in plugins Decoupling applications with domain events Domain Events

Slide 34

Slide 34 text

Thank you Get this presentation on slideshare: http://www.slideshare.com/cjsaylor/event-driven-application