Introduction to hexagonal architecture

Preview:

Citation preview

Hexagonal ArchitectureDecouple your code

About me

MANEL SELLÉS

Software engineerat Ulabox

FIBer

@manelselles

Hexagonal architecture

ComplexSystems

Hexagonal architecture

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

Web & apps Integrations & Backoffice Warehouse Delivery

Servers

Hexagonal architecture

ComplexApplications

Hexagonal architecture

Hexagonal architecture

WTF??

Hexagonal architecture

Model-View-Controller(MVC)

Hexagonal architecture

Hexagonal architecture

class Controller{ public function index() {

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

Hexagonal architecture

What if I want to access from command line?

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); }}

Hexagonal architecture

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

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)); }}

Hexagonal architecture

My boss asks me to change the logic of

$someStrangeValue

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)); }}

Hexagonal architecture

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

Hexagonal architecture

Dependency Injection to the rescue!

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()); }}

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())); }}

Hexagonal architecture

What if I decide to change my StrangeCalculator

database?

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; }}

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);

Hexagonal architecture

Welcome to Hexagonal Architecture!

akka ports & adapters

Hexagonal architecture

ThanksMake your code independent

Recommended