Top Banner
Hexagonal Architecture Decouple your code
26

Introduction to hexagonal architecture

Apr 13, 2017

Download

Software

Manel Sellés
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to hexagonal architecture

Hexagonal ArchitectureDecouple your code

Page 2: Introduction to hexagonal architecture

About me

MANEL SELLÉS

Software engineerat Ulabox

FIBer

@manelselles

Page 3: Introduction to hexagonal architecture

Hexagonal architecture

ComplexSystems

Page 4: Introduction to hexagonal architecture

Hexagonal architecture

http://www.slideshare.net/RubnSospedra/learning-from-the-ulabox-stack

Web & apps Integrations & Backoffice Warehouse Delivery

Servers

Page 5: Introduction to hexagonal architecture

Hexagonal architecture

ComplexApplications

Page 6: Introduction to hexagonal architecture

Hexagonal architecture

Page 7: Introduction to hexagonal architecture

Hexagonal architecture

WTF??

Page 8: Introduction to hexagonal architecture

Hexagonal architecture

Model-View-Controller(MVC)

Page 9: Introduction to hexagonal architecture

Hexagonal architecture

Page 10: Introduction to hexagonal architecture

Hexagonal architecture

class Controller{ public function index() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ render_html('index_view.php', $someStrangeValue); }}

Page 11: Introduction to hexagonal architecture

Hexagonal architecture

What if I want to access from command line?

Page 12: Introduction to hexagonal architecture

Hexagonal architecture

class Controller{ public function index() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ render_html('index_view.php', $someStrangeValue); }}

class CommandLine{ public function execute() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ print($someStrangeValue); }}

Page 13: Introduction to hexagonal architecture

Hexagonal architecture

What if I want to publish it to a messaging queue?

Page 14: Introduction to hexagonal architecture

Hexagonal architecture

class Controller{ public function index() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ render_html('index_view.php', $someStrangeValue); }}

class CommandLine{ public function execute() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ print($someStrangeValue); }}

class QueuePublisher{ public function publish() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ publish(new Message($someStrangeValue)); }}

Page 15: Introduction to hexagonal architecture

Hexagonal architecture

My boss asks me to change the logic of

$someStrangeValue

Page 16: Introduction to hexagonal architecture

Hexagonal architecture

class Controller{ public function index() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ render_html('index_view.php', $someStrangeValue); }}

class CommandLine{ public function execute() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ print($someStrangeValue); }}

class QueuePublisher{ public function publish() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ publish(new Message($someStrangeValue)); }}

Page 17: Introduction to hexagonal architecture

Hexagonal architecture

What can we do?1. Live with duplication?2. Encapsulate in a service?

Page 18: Introduction to hexagonal architecture

Hexagonal architecture

Dependency Injection to the rescue!

Page 19: Introduction to hexagonal architecture

Hexagonal architecture

class StrangeCalculator{ public function calculate() {

$someStrangeValue = ...; /** Some operation with data from MySQL **/ return $someStrangeValue; }}

class Controller{

private $strangeCalculator;

public function __construct(StrangeCalculator $strangeCalculator) {

$this->strangeCalculator = $strangeCalculator; } public function index() { render_html('index_view.php', $this->strangeCalculator->calculate()); }}

Page 20: Introduction to hexagonal architecture

Hexagonal architecture

class CommandLine{

private $strangeCalculator;

public function __construct(StrangeCalculator $strangeCalculator) {

$this->strangeCalculator = $strangeCalculator; } public function execute() { print($this->strangeCalculator->calculate()); }}

class QueuePublisher{

private $strangeCalculator;

public function __construct(StrangeCalculator $strangeCalculator) {

$this->strangeCalculator = $strangeCalculator; } public function publish() { publish(new Message($this->strangeCalculator->calculate())); }}

Page 21: Introduction to hexagonal architecture

Hexagonal architecture

What if I decide to change my StrangeCalculator

database?

Page 22: Introduction to hexagonal architecture

Hexagonal architecture

interface StrangeCalculator{ public function calculate();}

class StrangeCalculatorFromMySQL implements StrangeCalculator{ public function calculate() {

$someStrangeValue = ...; /** get value from google **/ return $someStrangeValue; }}

class StrangeCalculatorWithMongoDB implements StrangeCalculator{ public function calculate() {

$someStrangeValue = ...; /** get value from mongo DB **/ return $someStrangeValue; }}

Page 23: Introduction to hexagonal architecture

Hexagonal architecture

class Controller{

private $strangeCalculator;

public function __construct(StrangeCalculator $strangeCalculator) {

$this->strangeCalculator = $strangeCalculator; } public function index() { render_html('index_view.php', $this->strangeCalculator->calculate()); }}

//Use controller with StrangeCalculatorFromMySQL

$strangeCalculatorFromMySQL = new StrangeCalculatorFromMySQL();new Controller($strangeCalculatorFromMySQL);

//Use controller with StrangeCalculatorWithMongoDB

$strangeCalculatorWithMongoDB = new StrangeCalculatorWithMongoDB();new Controller($strangeCalculatorWithMongoDB);

Page 24: Introduction to hexagonal architecture

Hexagonal architecture

Welcome to Hexagonal Architecture!

akka ports & adapters

Page 25: Introduction to hexagonal architecture

Hexagonal architecture

Page 26: Introduction to hexagonal architecture

ThanksMake your code independent