Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hexagonal Architecture, or how to completely overengineer your application

Hexagonal Architecture, or how to completely overengineer your application

With this slidedeck, I try to explain what Hexagonal Architecture is. The main goal is to increase you knowledge about the subject, so that unfounded generalisations based on ignorance are a thing from the past!

Tom Van Herreweghe

September 27, 2016
Tweet

More Decks by Tom Van Herreweghe

Other Decks in Programming

Transcript

  1. ON THE MENU TODAY An Introduction Small Amount of Interaction

    Mainly Me, Explaining Things Illustrated By Some Code An O er of Links and Tools Small Amount of Interaction
  2. Who considers themselves still at the spaguetti level? Who considers

    themselves at the framework level? Who considers themselves at a further level? SMALL AMOUNT OF INTERACTION
  3. Who remembers their reaction when rst being introduced to a

    framework? SMALL AMOUNT OF INTERACTION
  4. The Hexagonal Architecture allows an application to be equally driven

    by users, programs, automated tests or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases
  5. HTTP Message ➞ Web Server ➞ PHP-FPM CLI Command ➞

    PHP Binary ➞ PHP Script ALREADY PART OF YOUR APPLICATION
  6. (or how to not leak infrastructure details) Depend on I

    n t e r f a c e Inject concrete implementation THE DEPENDENCY INVERSION PRINCIPLE
  7. < ? p h p n a m e s

    p a c e F o o \ B a r \ D o m a i n \ S e r v i c e ; u s e F o o \ B a r \ I n f r a s t r u c t u r e \ R e p o s i t o r y \ U s e r M y S q l R e p o s i t o r y ; c l a s s U s e r S e r v i c e { p u b l i c f u n c t i o n _ _ c o n s t r u c t ( U s e r M y S q l R e p o s i t o r y $ r e p o ) { / / . . . } }
  8. < ? p h p n a m e s

    p a c e F o o \ B a r \ D o m a i n \ S e r v i c e ; u s e F o o \ B a r \ D o m a i n \ R e p o s i t o r y \ U s e r R e p o s i t o r y ; c l a s s U s e r S e r v i c e { p u b l i c f u n c t i o n _ _ c o n s t r u c t ( U s e r R e p o s i t o r y $ r e p o ) { / / . . . } }
  9. < ? p h p n a m e s

    p a c e F o o \ B a r \ I n f r a s t r u c t u r e \ R e p o s i t o r y \ U s e r M y S q l R e p o s i t o r y ; u s e F o o \ B a r \ D o m a i n \ R e p o s i t o r y \ U s e r R e p o s i t o r y ; c l a s s U s e r M y S q l R e p o s i t o r y i m p l e m e n t s U s e r R e p o s i t o r y { p u b l i c f u n c t i o n _ _ c o n s t r u c t ( U s e r R e p o s i t o r y $ r e p o ) { / / . . . } }
  10. Create a Message in the Infrastructure layer Use a Port

    to get it to the Domain layer An Adapter creates something useful ↳ Your Domain code CROSSING BOUNDARIES IN YOUR APPLICATION
  11. < ? p h p c l a s s

    S a v e B l o g P o s t { p r i v a t e $ t i t l e ; p r i v a t e $ c o n t e n t ; p r i v a t e $ t a g s = [ ] ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( $ t i t l e , $ c o n t e n t , $ t a g s ) { $ t h i s - > t i t l e = $ t i t l e ; $ t h i s - > c o n t e n t = $ c o n t e n t ; $ t h i s - > t a g s = $ t a g s ; } }
  12. < ? p h p c l a s s

    S a v e B l o g P o s t H a n d l e r { p r i v a t e $ r e p o ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( B l o g P o s t R e p o s i t o r y $ r e p o ) { $ t h i s - > r e p o = $ r e p o ; } p u b l i c f u n c t i o n h a n d l e ( S a v e B l o g P o s t $ c o m m a n d ) { $ p o s t = B l o g P o s t : : c r e a t e N e w ( $ c o m m a n d - > g e t T i t l e ( ) , $ c o m m a n d - > g e t C o n t e n t ( ) ) ; $ p o s t - > a d d T a g s ( $ c o m m a n d - > g e t T a g s ( ) ) ; $ t h i s - > r e p o - > a d d ( $ p o s t ) ; } }
  13. < ? p h p c l a s s

    B l o g A d m i n C o n t r o l l e r e x t e n d s B a s e C o n t r o l l e r { p u b l i c f u n c t i o n c r e a t e A c t i o n ( $ r e q u e s t ) { / / c r e a t e f o r m e t c . i f ( $ r e q u e s t - > i s P o s t ( ) ) { $ c o m m a n d = n e w S a v e B l o g P o s t ( $ r e q u e s t - > g e t ( ' t i t l e ' ) , $ r e q u e s t - > g e t ( ' c o n t e n t ' ) , $ r e q u e s t - > g e t ( ' t a g s ' , [ ] ) ) ; $ h a n d l e r = $ t h i s - > g e t ( ' s a v e _ b l o g p o s t _ h a n d l e r ' ) ; $ h a n d l e r - > h a n d l e ( $ c o m m a n d ) ; $ r e s p o n s e - > r e d i r e c t ( ' / f o o ' ) ; } / / d i s p l a y t e m p l a t e
  14. < ? p h p c l a s s

    B l o g A d m i n C o n t r o l l e r { p u b l i c f u n c t i o n c r e a t e A c t i o n ( $ r e q u e s t ) { / / c r e a t e f o r m e t c . i f ( $ r e q u e s t - > i s P o s t ( ) ) { $ c o m m a n d = n e w S a v e B l o g P o s t ( $ r e q u e s t - > g e t ( ' t i t l e ' ) , $ r e q u e s t - > g e t ( ' c o n t e n t ' ) , $ r e q u e s t - > g e t ( ' t a g s ' , [ ] ) ) ; $ t h i s - > c o m m a n d B u s - > h a n d l e ( $ c o m m a n d ) ; $ r e s p o n s e - > r e d i r e c t ( ' / f o o ' ) ; } / / d i s p l a y t e m p l a t e } }
  15. < ? p h p c l a s s

    B l o g A d m i n C o n t r o l l e r { p u b l i c f u n c t i o n c r e a t e A c t i o n ( $ r e q u e s t ) { / / c r e a t e f o r m e t c . i f ( $ r e q u e s t - > i s P o s t ( ) ) { $ i d = $ t h i s - > g e n e r a t o r - > g e n e r a t e I d ( ) ; $ c o m m a n d = n e w S a v e B l o g P o s t ( $ i d , $ r e q u e s t - > g e t ( ' t i t l e ' ) , $ r e q u e s t - > g e t ( ' c o n t e n t ' ) , $ r e q u e s t - > g e t ( ' t a g s ' , [ ] ) ) ; $ t h i s - > c o m m a n d B u s - > h a n d l e ( $ c o m m a n d ) ; $ r e s p o n s e - > r e d i r e c t ( ' / d e t a i l s / ' . $ i d ) ; }
  16. C r e a t e U s e r

    & E d i t U s e r ⇨ S a v e U s e r Reusable Easier to maintain SMART NAMING
  17. Allistair Cockburn - Hexagonal Architecture - http://alistair.cockburn.us/Hexagonal+architecture Tactician - Command

    Bus - https://tactician.thephpleague.com/ SimpleBus - Command Bus & more - http://simplebus.github.io/MessageBus/ Matthias Noback - Slides PHPBenelux 2016 DDD in PHP - https://leanpub.com/ddd-in-php TOOLS / LIBRARIES