Is your code highly coupled to delivery mechanisms?

About me

Luciano Queiroz

  • Recife / PE
     
  • PHP Software Engineer ~7 years
     
  • Work remotely ~4 years
     
  • First talk
     
  • Motivations
     
  • Community

Delivery Mechanisms

  • Web application
     
  • Console application
     
  • Database
     
  • ORM
     
  • SoA
     
  • Messages Queue

Delivery Mechanisms

Starting a new project

Some common steps

  • Web Framework
     
  • Set a database
     
  • Create Schema / Migration
     
  • Create Model
     
  • Create Controller
     
  • Create View

success! :)

"Businesses regularly put too much effort into developing glorified database table editors."

Vaughn Vernon, Implementing Domain Driven Design (Perason Education, Inc: 2013), 172.

View

Controller

Model

Database

Data

Data

Data

Anemic Models

Memory Loss

<?php
App::uses('AppController', 'Controller');

class CustomersController extends AppController {

	public function add() {
		if ($this->request->is('post')) {
			$this->Customer->create();
			if ($this->Customer->save($this->request->data)) {
				$this->Flash->success(__('The customer has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The customer could not be saved. Please, try again.'));
			}
		}
	}

	public function edit($id = null) {
		if (!$this->Customer->exists($id)) {
			throw new NotFoundException(__('Invalid customer'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Customer->save($this->request->data)) {
				$this->Flash->success(__('The customer has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The customer could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Customer.' . $this->Customer->primaryKey => $id));
			$this->request->data = $this->Customer->find('first', $options);
		}
	}
}

Coupling to Controller and ORM 

<?php
App::uses('AppController', 'Controller');

class CustomersController extends AppController {

	public function add() {
		if ($this->request->is('post')) {
			$this->Customer->create();
			if ($this->Customer->save($this->request->data)) {
				$this->Flash->success(__('The customer has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The customer could not be saved. Please, try again.'));
			}
		}
	}

	public function edit($id = null) {
		if (!$this->Customer->exists($id)) {
			throw new NotFoundException(__('Invalid customer'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Customer->save($this->request->data)) {
				$this->Flash->success(__('The customer has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The customer could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Customer.' . $this->Customer->primaryKey => $id));
			$this->request->data = $this->Customer->find('first', $options);
		}
	}
}

Problems found

  • Difficult/slow to test
     
  • Reusability almost impossible
     
  • It only works in web environment
     
  • Difficult to run multiple times
     
  • Dependency on concretions and not abstractions

Screaming Architecture

"So what does the architecture of your application scream? When you look at the top level directory structure, and the source files in the highest level package; do they scream: Health Care System, or Accounting System, or Inventory Management System? Or do they scream: Rails, or Spring/Hibernate, or ASP?"

Uncle Bob

Layered Architecture

"Develop a design within each layer that is cohesive and that depends only on the layers below."

[Evans, Ref, p.16]

User Interface

Application Layer

Layered Architecture

Infrastructure

Domain Layer

Reorganizing

Application Layer

Domain Layer

Layered Architecture

Infrastructure

Dependency Inversion Principle

Depend on abstractions, not on concretions.

Application Layer

Domain Layer

Layered Architecture

Infrastructure

Ain't no layers anymore!!!

Layered Architecture

Application

Domain

Infrastructure

Layered Architecture with DIP

Domain

Entity

Value Object

Application

Infrastructure

Why frameworks can't help?

Hexagonal

"Create your application to work without either a UI or a database so you can run automated regression-tests against the application, work when the database becomes unavailable, and link applications together without any user involvement"

Alistair Cockburn.

Hexagonal ):
Octogonal

Domain

Application

Infrastructure

Http

Persistence

AMPQ

Console

Http Port / Adapter

Http

Request

Command

Handler

ServiceBus

Entities

Value Objects

Http Land

Domain Land

Command

<?php

class RegisterCustomer {

    protected $name;
    protected $email;
    protected $username;
    protected $password;
    
    private function __construct(
        $name, $email, $username, $password
    ) {
        $this->name = $name,
        $this->email = $email;
        $this->username = $username;
        $this->password = $password;
    }
    
    public static function fromRequest(
        $name, $email, $username, $password
    ) {
        return new self(
            $name, $email, $username, $password
        )
    }

}

CommandHandler

<?php

class RegisterCustomerHandler {

    private $command;
    private $repository;
    
    public function __construct(
        IRegisterUser $command,
        ICustomerRepository $repository
    )
    {
        $this->command = $name,
        $this->repository = $repository;
    }
    
    public function handle()
    {
        $customer = new Customer(
            $this->command->name,
            UserName::fromString(
                $this->command->username
            ),
            Email::fromString(
                $this->command->email
            )
        )
        
        return $this->repository->add($customer);
    }
}

Adapter / Persistence Port 

IRepository

ORMRepository

MongoDB

MySQL

Domain Land

Persistence Land

ODMRepository

InMemoryRepository

CacheRepository

Redis

IRepository

ICustomerRepository

<?php

interface ICostumerRepository {
    public function add();
}

CustomerRepository

<?php

class CustomerRepository extends EntityRepository implements ICustomerRepository {
    public function add(Customer $customer)
    {
        $this->em->persist($customer);
        $this->em->flush();
    }
}

Why Hexagonal?

Advantages using Hexagonal

  • Faster tests
     
  • Code first
     
  • Focus on Use-Cases
     
  • Bounded Context

http://geeknightrecife.github.io

References

  • Implementing Domain Driven Design - Vaughn Vernon.
     

  • Domain-Driven Design: Tackling Complexity in the Heart of Software - Eric Evans.
     

  • Principles of Package Design - Mathias Noback.
     

  • Clean Code Episode VII - Architecture, Use Cases, and High Level Design - Uncle Bob.
     

  • Writing Effective Use Cases - Alistair Cockburn.
     

  • http://alistair.cockburn.us/Hexagonal+architecture

Contacts

  • @luciianoqueiroz
     

  • luciiano.queiroz@gmail.com

Questions?

Thanks! :)

https://joind.in/talk/f142f

Is your code highly coupled to delivery mechanisms?

By Luciano Queiroz

Is your code highly coupled to delivery mechanisms?

Slide para o evento PHPeste Salvador 2016.

  • 1,381