Slide 1

Slide 1 text

SERVICE ORIENTED ARCHITECTURE Midwest PHP 2015

Slide 2

Slide 2 text

I AM MIKE WILLBANKS • Father, Husband, Developer • VP of Development at Packet Power • Twitter: @mwillbanks • ZF Contributor, ZF CR Team, ZF Certification Advisory Group

Slide 3

Slide 3 text

Applications much like onions, have multiple layers.

Slide 4

Slide 4 text

A STORY

Slide 5

Slide 5 text

ONCE UPON A TIME We wrote procedural code…

Slide 6

Slide 6 text

Recent Books
    select('...'); foreach ($books as $book) { echo '
  • ' . $book['title'] . '
  • '; } ?>

Slide 7

Slide 7 text

insert('book', array( 'title' => $_POST['title'], )); } ?> Add Book

Slide 8

Slide 8 text

ALL WAS FINE IN THE WORLD Except for what was playing out before us…

Slide 9

Slide 9 text

WE NEED LIBRARY The boss tells you it’s time for the library feature… and we need to update books.

Slide 10

Slide 10 text

My Libraries

Slide 11

Slide 11 text

insert('library', array( 'name' => $_POST['name'], )); } ?> Add Library

Slide 12

Slide 12 text

select('...'); $book = $rs[0]; if ($book && $_SERVER['REQUEST_METHOD'] == 'POST') { $db->update('book', array( 'library_id' => $_POST['library_id'], 'title' => $_POST['title'], ), array( 'book_id' => $book['id'], )); } $libraries = $db->select('...'); ?> Update Book

Slide 13

Slide 13 text

And these things continued…

Slide 14

Slide 14 text

EVERYTHING IS FALLING APART

Slide 15

Slide 15 text

BUGS All these little demons came out of the groundwork.

Slide 16

Slide 16 text

#!/bin/bash grep -lri bookId * | wc -l $ 271

Slide 17

Slide 17 text

REWRITE! Or I’m done!

Slide 18

Slide 18 text

MVC Model View Controller Model Controller View

Slide 19

Slide 19 text

MVC REQUEST LIFECYCLE

Slide 20

Slide 20 text

CONTROLLERS • Send commands to the model and sends information to the view. • It’s where your request and response are handled.

Slide 21

Slide 21 text

model->insert(array( 'title' => $this->getPost('title') )); return array( 'book' => $book ); } }

Slide 22

Slide 22 text

MODELS • Handles the state, persistence, data access and more. • Might have mappers and entities, it might not.. • It’s a loaded term.

Slide 23

Slide 23 text

db->insert('book', $book); return $book; } }

Slide 24

Slide 24 text

Slide 25

Slide 25 text

Slide 26

Slide 26 text

VIEWS • Separating display from business logic • Escaping and handling what our controller is providing

Slide 27

Slide 27 text

layout('main'); ?>
  • escape($book->getTitle());?>

Slide 28

Slide 28 text

THERE IS A BETTER WAY.

Slide 29

Slide 29 text

SOA Service Oriented Architecture Service Layer Controller View Domain Models Data Mappers Data Access Objects Model

Slide 30

Slide 30 text

BENEFITS OF A SERVICE LAYER • Better Controllers • Better Maintainability • Better Parallelism in Development • Better Testing/Fewer Defects • More Reuse

Slide 31

Slide 31 text

LETS GET BUILDING

Slide 32

Slide 32 text

BASIC PRINCIPLES

Slide 33

Slide 33 text

CONTROLLERS Exactly like MVC, but injecting a service layer.

Slide 34

Slide 34 text

bookService->addBook( $this->getPost('isbn'), $this->getPost('title'), $this->getPost('description') ); return $this->redirect('/book/' . $book- >getId()); } }

Slide 35

Slide 35 text

SERVICES • Your application logic / business functionality • The in-between layer • The internal API to your application

Slide 36

Slide 36 text

– Wikipedia http://en.wikipedia.org/wiki/Service_(systems_architecture) “A set of related software functionalities that can be reused for different purposes.”

Slide 37

Slide 37 text

setIsbn($isbn); $book->setTitle($title); $book->setDescription($description); $this->mapper->addBook($book); return $book; } }

Slide 38

Slide 38 text

DOMAIN MODELS • Describe a “thing” • Data object • Domain logic • Generally getters, setters

Slide 39

Slide 39 text

Slide 40

Slide 40 text

FILTERING AND VALIDATION • Always filter • User data to be validated.

Slide 41

Slide 41 text

filter($title); $this->title = title; return $this; } }

Slide 42

Slide 42 text

filter($title); $validate = new AlnumValidator(); if (!$validate->isValid($title)) { throw new InvalidArgumentException($validate->getMessage()); } $this->title = title; return $this; } }

Slide 43

Slide 43 text

INPUT FILTER PATTERN Merge filters and validators

Slide 44

Slide 44 text

getFilterChain() ->attachByName('stringtrim') ->attachByName('alphanumeric'); $input->getValidatorChain() ->attachByName('not_empty') ->attachByName('string_length', array('min' => 1, 'max' => 45)); $this->inputFilter->addInput($input); return $this->inputFilter; } }

Slide 45

Slide 45 text

getInputFilter()- >getInput('title'); $inputFilter->setValue($title); if ($inputFilter->isValid()) { $this->title = $title; } throw new InvalidArgumentException($inputFilter- >getMessages()); } }

Slide 46

Slide 46 text

DATA MAPPERS • Separates mapping data to a data store as well as your data model. • Data store can be anything or even multiple things.

Slide 47

Slide 47 text

repository->save($book); return $book; } }

Slide 48

Slide 48 text

But I have multiple tables for this entity!!!

Slide 49

Slide 49 text

Slide 50

Slide 50 text

bookRepository->save($book); $this->bookDetails->save($book); } }

Slide 51

Slide 51 text

DUPLICATION?! Not Exactly

Slide 52

Slide 52 text

We need to lower our database load, let’s cache.

Slide 53

Slide 53 text

cache->has('book.recent')) { return $this->cache->get('book.recent'); } $results = $this->repository->findRecent(); $this->cache->set('book.recent', $results); return $results; } }

Slide 54

Slide 54 text

But that seems messy.

Slide 55

Slide 55 text

DATA ACCESS OBJECTS • Or repositories in this case. • Allow us to target other areas to store data. • Allow for far better decoration • Things are looking up.

Slide 56

Slide 56 text

getId()) { return $this->insert('book', $book); } return $this->update('book', $book, array( 'id' => $book->getId() )); } }

Slide 57

Slide 57 text

Slide 58

Slide 58 text

Slide 59

Slide 59 text

DECORATION

Slide 60

Slide 60 text

DECORATOR PATTERN • Used to extend the functionality of a certain object. • Ultimately wrapping

Slide 61

Slide 61 text

gateway->select(...); return $book; } }

Slide 62

Slide 62 text

cache->has('book.recent')) { return $this->cache->get('book.recent'); } $results = $this->delegateRepository- >findRecent(); $this->cache->set('book.recent', $results); return $results; } }

Slide 63

Slide 63 text

WHAT ARE WE ACHIEVING • Discoverability • Maintainability • Readability

Slide 64

Slide 64 text

DEPENDENCY INJECTION Keeping everything alive.

Slide 65

Slide 65 text

Dependency injection is the concept of simply injecting anything that is necessary for that object to operate.

Slide 66

Slide 66 text

Slide 67

Slide 67 text

DI CONTAINERS • Inject dependencies by use of a container.

Slide 68

Slide 68 text

return array( 'di' => array( 'BookService' => array( 'arguments' => array( 'BookMapper', ), ), 'BookMapper' => array( 'arguments' => array( 'BookCacheRepository', ), ), 'BookCacheRepository' => array( 'arguments' => array( 'BookDbRepository', ), ), 'BookDbRepository' => array( 'arguments' => array( 'TableGateway', ),

Slide 69

Slide 69 text

SO WHAT DOES THAT DO? • Creates an instance of TableGateway • Creates an instance of BookDbRepository • Then an instance of BookCacheRepository • Then an instance of BookMapper • Finally, an instance of BookService get( ‘BookService’ );

Slide 70

Slide 70 text

CLI? Oh, yes. We can!

Slide 71

Slide 71 text

get('book.service'); $bookService->addBook($argv[0], $argv[1], $argv[2]);

Slide 72

Slide 72 text

THAT’S SEEMS A LITTLE SLIMY… BUT, there is nothing preventing us from using controllers.

Slide 73

Slide 73 text

get('cliRouter'); } else { $router = $container->get('router'); } class BookCliController extends AbstractCliController { public function addBookAction(); }

Slide 74

Slide 74 text

BUT WHAT BELONGS? • Orchestration • Security • Transactions

Slide 75

Slide 75 text

DOMAIN EVENTS A kitchen full of examples…

Slide 76

Slide 76 text

setIsbn($isbn); $book->setTitle($title); $book->setDescription($description); $this->mapper->addBook($book); $this->trigger('book.add', array('book' => $book)); return $book; } }

Slide 77

Slide 77 text

attach('book.add', function($e) use ($mq) { $params = $e->getParams(); $book = $params['book']; $mq->sendMessage('book.add', array('book' => $book)); });

Slide 78

Slide 78 text

WEB SERVICES Practically Free!

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

JEFF BEZOS MANDATE • All teams will henceforth expose their data and functionality through service interfaces. • Teams must communicate with each other through these interfaces. • There will be no other form of interprocess communication allowed: no direct linking, no direct reads of another team’s data store, no shared-memory model, no back-doors whatsoever. The only communication allowed is via service interface calls over the network. • It doesn’t matter what technology they use. HTTP, Corba, Pubsub, custom protocols — doesn’t matter. • All service interfaces, without exception, must be designed from the ground up to be externalizable. That is to say, the team must plan and design to be able to expose the interface to developers in the outside world. No exceptions. • Anyone who doesn’t do this will be fired. • Thank you; have a nice day!

Slide 81

Slide 81 text

REST

Slide 82

Slide 82 text

get('/api/book', 'ApiBookController.recentAction'); $app->put('/api/book', 'ApiBookController.addAction'); class ApiBookController extends RestfulController { public function recentAction() { return $this->bookService->findRecent(); } public function addAction() { return $this->bookService->addBook(...); } }

Slide 83

Slide 83 text

SOAP

Slide 84

Slide 84 text

setClass('BookService'); $server->setObject(new BookService()); $server->handle();

Slide 85

Slide 85 text

RPC

Slide 86

Slide 86 text

setClass('BookService'); $server->handle(); setClass('BookService'); echo $server->handle();

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

• http://pixabay.com/en/architecture-iron-steel-building-414035/ • http://pixabay.com/en/home-demolition-destroyed-broken-557297/ • http://pixabay.com/en/disassembly-component-parts-336507/ • http://pixabay.com/en/taps-thread-drill-milling-444473/ • http://pixabay.com/en/light-bulb-transparent-light-body-629661/ • http://pixabay.com/en/church-trappist-georgia-monastery-233564/ • http://pixabay.com/en/lego-site-build-replica-516557/ • http://pixabay.com/en/water-tap-wall-outside-exterior-243046/ • http://pixabay.com/en/question-mark-note-duplicate-457453/ • http://pixabay.com/en/door-entry-stone-opening-grid-208081/ • http://pixabay.com/en/onion-cut-fresh-tears-vegetable-161611/ • http://pixabay.com/en/petronas-towers-skyscraper-433081/ • http://pixabay.com/en/carbine-rope-hook-backup-climbing-7104/ • http://pixabay.com/en/code-program-source-computer-pc-113611/ • http://pixabay.com/en/kitchen-cabinets-countertop-granite-670247/ • http://pixabay.com/en/injecting-medical-shot-veins-519389/ • http://pixabay.com/en/books-shelf-retro-black-and-white-635341/ • http://pixabay.com/en/crayons-pencils-blurred-circle-311062/ • http://pixabay.com/en/soap-curd-soap-bathing-cleaning-390300/ • http://pixabay.com/en/helicopter-model-model-helicopter-195660/ • http://pixabay.com/en/bricks-roofing-house-building-420725/ • http://pixabay.com/en/parrot-ara-zoo-bird-beautiful-bill-655777/ • http://pixabay.com/en/gorilla-silverback-animal-448731/ • http://pixabay.com/en/crash-demolition-site-home-floors-214744/ • http://pixabay.com/en/beetle-beasts-field-insect-431125/ • http://en.wikipedia.org/wiki/File:MVC-Process.svg • http://pixabay.com/en/prairie-dog-small-cute-rodent-keep-320836/ • http://pixabay.com/en/key-lock-door-security-home-565607/ • http://pixabay.com/en/library-inside-wood-book-books-428034/ • http://pixabay.com/en/book-old-antique-pages-empty-pages-657637/ • http://pixabay.com/en/dirt-mess-messy-grungy-face-576491/ • http://pixabay.com/en/antique-box-collectible-electronic-21935/ • http://pixabay.com/en/processor-cpu-board-circuits-540254/ • http://pixabay.com/en/slug-shell-animals-wildlife-87037/ • http://pixabay.com/en/colour-pencils-color-paint-draw-450623/ • http://pixabay.com/en/mount-st-helens-volcanic-eruption-164848/ • http://pixabay.com/en/baby-tears-small-child-sad-cry-443393/ • http://pixabay.com/en/children-win-success-video-game-593313/ • http://pixabay.com/en/peas-pod-pea-pod-green-fresh-580333/ • http://pixabay.com/en/site-construction-work-support-592458/ • http://pixabay.com/en/chimney-sweeper-chimney-sweep-14035/ Image Credits THANK YOU! http://joind.in/talk/view/13838