Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Hexagonal Architecture
Search
Chris Fidao
May 16, 2014
Technology
49
200k
Hexagonal Architecture
An explanation on what Hexagonal Architecture is - the decoupling of layers in your code.
Chris Fidao
May 16, 2014
Tweet
Share
More Decks by Chris Fidao
See All by Chris Fidao
Development Environments that Feel Local
fideloper
0
30
Refactoring Terraform - CloudCasts - Scaling EC2
fideloper
0
53
Scaling Laravel - Laracon.net 2018
fideloper
15
1.8k
Linux Environment
fideloper
1
10k
Server Survival
fideloper
29
23k
FileBeat (Won't save you from the JVM)
fideloper
1
300
Powering Your Applications With Nginx
fideloper
9
7.7k
Intro to etcd
fideloper
3
540
Service Oriented Architecture with a little help from NodeJS
fideloper
4
2.3k
Other Decks in Technology
See All in Technology
20241214_WACATE2024冬_テスト設計技法をチョット俯瞰してみよう
kzsuzuki
3
470
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
200
Opcodeを読んでいたら何故かphp-srcを読んでいた話
murashotaro
0
250
ブラックフライデーで購入したPixel9で、Gemini Nanoを動かしてみた
marchin1989
1
540
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
2024年にチャレンジしたことを振り返るぞ
mitchan
0
140
終了の危機にあった15年続くWebサービスを全力で存続させる - phpcon2024
yositosi
12
11k
どちらを使う?GitHub or Azure DevOps Ver. 24H2
kkamegawa
0
800
re:Invent をおうちで楽しんでみた ~CloudWatch のオブザーバビリティ機能がスゴい!/ Enjoyed AWS re:Invent from Home and CloudWatch Observability Feature is Amazing!
yuj1osm
0
120
大幅アップデートされたRagas v0.2をキャッチアップ
os1ma
2
540
あの日俺達が夢見たサーバレスアーキテクチャ/the-serverless-architecture-we-dreamed-of
tomoki10
0
460
社内イベント管理システムを1週間でAKSからACAに移行した話し
shingo_kawahara
0
190
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
17
2.3k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Site-Speed That Sticks
csswizardry
2
190
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Making Projects Easy
brettharned
116
5.9k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Transcript
Hexagonal Architecture Chris Fidao (hek-sag-uh-nl)
@fideloper
None
Implementing Laravel Real-world implementation of testable and maintainable code. (hopefully)
Vaprobash Vagrant Provisioning Bash Scripts
Servers for Hackers.com
Why / What Ports / Adapters Boundary Layers /
WHY Architecture
Maintainability Technical Debt Time
What is it?
So…What is it?
The Hexagon Core Domain Application Domain Framework
(Core) Domain Core Domain Application Domain Framework
Behavior
Constraints
Application Core Domain Application Domain Framework
Framework Core Domain Application Domain Framework
Outside Core Domain Application Domain Framework
Ports Adapters /
Ports & Adapters Core Domain Application Domain Framework
Inside/Outside Core Domain Application Domain CommandBus Framework HTTP Use Case
Repo DBAL Database Events Dispatcher Service Impl
Core Domain Application Domain Framework Dependencies
interface Notifier { ! public function send(Message $message); } class
SesNotifier implements Notifier { ! public function send(Message $message) { // Details } }
Use-Case Driven Development
All the Contexts •Web •API •CLI •Queue •Event Handler
Use Cases: CommandBus CommandBus executes( ) Command Handler handles( )
Command
// Class SimpleCommandBus ! public function execute( $command ) {
return $this->getHandler( $command ) ->handle( $command ); } Simple CommandBus
Core Domain Application Domain CommandBus Framework HTTP Use Case Repo
DBAL Database Events Dispatcher Service Impl
None
Boundaries
Domain/Application Boundary Core Domain Application Domain Framework Use Case Repo
Events
interface CommandBusInterface { ! public function execute( $command ); }
interface HandlerInterface { ! public function handle( $command ); }
Core Domain Application Domain Framework Use Case Repo Events
interface TicketRepositoryInterface { ! public function getStaffOpenTickets( Staffer $staffer, $limit=10);
! ! public function save(Ticket $model); }
Application Domain CommandBus Framework DBAL Dispatcher The Application/External Boundary
interface Notifier { ! public function send(Message $message); } interface
Validator { ! public function passes(Array $data); ! public function getErrors(); } interface Dispatcher { ! public function dispatch(Array $events); }
Framework Core Domain Application Domain Framework HTTP Database Service Impl
Identify the aspects that vary and separate them from what
stays the same
Layers
The Domain Core Domain Application Domain Framework Use Case Repo
Events
<?php namespace Hex\Tickets; ! class Ticket extends Model { !
public function assignStaffer(Staffer $staffer) { if( ! $staffer->categories->contains( $this->category ) ) { throw new DomainException("Staffer can't be assigned to ".$this->category); } ! $this->staffer()->associate($staffer); // Set Relationship ! return $this; } ! public function setCategory(Category $category) { if( $this->staffer instanceof Staffer && ! $this->staffer->categories->contains( $category ) ) { // Unset staffer if can't be assigned to set category $this->staffer = null; } ! $this->category()->associate($category); // Set Relationship ! return $this; } }
class Ticket extends Model { ! /* ... Other logic
... */ ! public function save(array $options = array()) { /* Integrity Checks, and then: */ ! if( ! $this->exists ) { $this->raise( new TicketCreatedEvent($this) ); } ! return parent::save($options); } }
class CreateTicketCommand { ! protected $data; ! public function __construct($data)
{ $this->data = $data; } ! public function __get($property) { // Simplified example return $this->data[$property]; } }
The Application Application Domain CommandBus Framework DBAL Dispatcher
// Class SimpleCommandBus ! public function execute( $command ) {
return $this->getHandler( $command ) ->handle( $command ); }
! class CreateTicketHandler implements HandlerInterface { ! ! public function
handle($command) { $this->validate($command); // Throw ValidationException $this->save($command); } ! protected function save($command) { $ticket = new Ticket; /* Some other setters... */ $ticket->setCategory( $this->catRepo->find($command->category_id) ); $ticket->setStaffer( $this->staffRepo->find($command->staffer_id) ); $ticket->addMessage( $ticket->addMessage($command->message); ); ! $this->ticketRepo->save($ticket); // Use Repositories ! $this->dispatcher->dispatch( $ticket->flushEvents() ); // Fire Events } }
class DbTicketRepository implements RepositoryInterface { ! public function getStaffOpenTickets(Staffer $staffer,
$limit=10) { return $this->ticket->where('staff_id', $staffer->id) ->take($limit)->get(); } ! public function save(Ticket $ticket) { $ticket->save(); } }
Framework Core Domain Application Domain Framework HTTP Database Service Impl
class TicketController extends BaseController { ! public function createTicket() {
$command = new CreateTicketCommand( Input::all() ); ! try { $this->bus->execute($command); } catch(ValidationException $e) { return Redirect::to('/tickets/new') ->withErrors( $e->getErrors() ); } catch(DomainException $e) { return Redirect::to('/tickets/new') ->withErrors( $e->getErrors() ); } return Redirect::to(‘/tickets'); } }
class SesEmailNotifier implements NotifierInterface { ! public function __construct(SesClient $client)
{ $this->client = $client; } ! public function send(Message $message) { $to = [$message->to()]; $message = ['Data' => $message->message()]; ! $this->client->sendEmail([ 'Destination' => ['ToAddresses' => $to], 'Message' => ['Body' => ['Html' => $message]] ]); } }
use Illuminate\Events\Dispatcher as EventDispatcher; ! class LaravelDispatcher implements Dispatcher {
! public function __construct(EventDispatcher $dispatcher) { $this->dispatcher = $dispatcher; } ! public function dispatch(Array $events) { foreach( $events as $event ) { $this->dispatcher->fire( $event->name(), $event ); } } ! }
TDD is DEAD (and other myths)
Identify the aspects that vary and separate them from what
stays the same
Thanks