Slide 1

Slide 1 text

DDD in PHP on example of Symfony

Slide 2

Slide 2 text

cystbear Symfony expert MongoDB adept Erlang fun OSS doer KNPer https://twitter.com/1cdecoder https://github.com/cystbear http://knplabs.com/

Slide 3

Slide 3 text

Learning Pyramid

Slide 4

Slide 4 text

PHP? ORLY?

Slide 5

Slide 5 text

What this talk about?

Slide 6

Slide 6 text

About useful tool/lib?

Slide 7

Slide 7 text

About success story?

Slide 8

Slide 8 text

No! It’s about idea Motivation!

Slide 9

Slide 9 text

MVC

Slide 10

Slide 10 text

Where to store business logic? Model View Controller

Slide 11

Slide 11 text

Where to store business logic? Model View Controller

Slide 12

Slide 12 text

Where to store business logic? Model View Controller

Slide 13

Slide 13 text

Where to store business logic? Model View Controller -- YEAH!

Slide 14

Slide 14 text

Welcome to Fat Stupid Ugly Controllers FSUC/FUC http://blog.astrumfutura.com/2008/12/the-m-in-mvc-why-models-are-misunderstood-and-unappreciated/ http://zendframework.ru/anonses/model-with-mvc http://habrahabr.ru/post/175465/

Slide 15

Slide 15 text

Anemic (Domain) Model http://www.martinfowler.com/bliki/AnemicDomainModel.html http://habrahabr.ru/post/224879/ “In essence the problem with anemic domain models is that they incur all of the costs of a domain model, without yielding any of the benefits.” Martin Fowler

Slide 16

Slide 16 text

Persistence Layer Model

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

What is Not MVC (phew!) Request / Response Framework HTTP Framework http://fabien.potencier.org/article/49/what-is-symfony2

Slide 19

Slide 19 text

What about model, persistence layer?

Slide 20

Slide 20 text

Meet Doctrine http://www.doctrine-project.org/ SQL -- DBAL + ORM MongoDB CouchDB OrientDB PHPCR ODM OXM

Slide 21

Slide 21 text

What is Inversion of control Service Locator Dependency Injection Container http://www.martinfowler.com/articles/injection.html http://fabien.potencier.org/article/11/what-is-dependency-injection

Slide 22

Slide 22 text

Services http://groovy.codehaus.org/ https://grails.org/ Single Class With its Deps (min) set Easy to Replace Easy to Test MVC(S)!

Slide 23

Slide 23 text

Controller’s pray https://twitter.com/ornicar Get Request Submit form if any Call one Service method Return Response Rendering HTML far away

Slide 24

Slide 24 text

Managers Managers Managers http://blog.codinghorror.com/i-shall-call-it-somethingmanager/ http://stackoverflow.com/questions/1866794/naming-classes-how-to- avoid-calling-everything-a-whatevermanager

Slide 25

Slide 25 text

Real Pain class BackendUserProgramsPossessionFormHandler { protected $dep1; // deps holder props public function __construct(DepsClass $dep1 /*, ...*/) { $this->dep1 = $dep1; } public function process(Form $form) { $this->dep1->makeHappy($form); // ... }

Slide 26

Slide 26 text

How Kris writes Symfony apps#44 https://twitter.com/kriswallsmith http://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps

Slide 27

Slide 27 text

https://twitter.com/kriswallsmith http://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps How Kris writes Symfony apps#44

Slide 28

Slide 28 text

Domain Logic Patterns http://martinfowler.com/books/eaa.html

Slide 29

Slide 29 text

Domain Logic Patterns http://martinfowler.com/books/eaa.html Transaction Script Domain Model Table Module Service Layer

Slide 30

Slide 30 text

Transaction Script

Slide 31

Slide 31 text

Domain Model

Slide 32

Slide 32 text

Table Module

Slide 33

Slide 33 text

Domain Logic & Application logic

Slide 34

Slide 34 text

Service Layer

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

What is next? RAD DDD Patterns Examples Layers Goodies

Slide 37

Slide 37 text

DDD != RAD Code First Do not Care about persistence (yet)

Slide 38

Slide 38 text

Domain Model Repository Value Object DTO Strategy State Patterns & Code

Slide 39

Slide 39 text

Domain Model

Slide 40

Slide 40 text

Domain Model id = $id; $this->name = $name; $this->recognitionStrategy = $recognitionStrategy; }

Slide 41

Slide 41 text

Repository

Slide 42

Slide 42 text

Value Object value = (string) $value; } public function getValue() { return $this->value; } }

Slide 43

Slide 43 text

Value Object DateRange

Slide 44

Slide 44 text

Value Object DateRange from = $from; $this->to = $to; } } public function findByDateRange(\DateRange $range)

Slide 45

Slide 45 text

Value Object Money

Slide 46

Slide 46 text

Slide 47

Slide 47 text

Slide 48

Slide 48 text

Strategy

Slide 49

Slide 49 text

______ ______ _______ _______ / | / __ \ | \ | ____| | ,----'| | | | | .--. || |__ | | | | | | | | | || __| | `----.| `--' | | '--' || |____ \______| \______/ |_______/ |_______|

Slide 50

Slide 50 text

src └── MegaCorp ├── ApiBundle │ ├── Controller │ │ └── ... │ └── MegaCorpApiBundle.php ├── Core │ └── Product │ ├── Product.php │ ├── ProductId.php │ └── ProductRepository.php └── CoreBundle ├── Controller │ └── ... ├── Repository │ ├── InMemoryProductRepository.php │ └── MongoDbProductRepository.php └── MegaCorpCoreBundle.php Directory structure

Slide 51

Slide 51 text

Layers

Slide 52

Slide 52 text

Layers Domain Layer -- heart of your application, Entities and Repositories Application Layer -- Controllers Presentation Layer -- Templates / DTOs for serializer Infrastructure Layer -- framework, persistence, concrete implementations of Domain Layer

Slide 53

Slide 53 text

Useful goodies

Slide 54

Slide 54 text

BBB DDD by Eric Evans http://amzn.com/0321125215/

Slide 55

Slide 55 text

DDD Quickly by InfoQ http://www.infoq.com/minibooks/domain-driven-design-quickly

Slide 56

Slide 56 text

PoEAA by Martin Fowler http://amzn.com/B008OHVDFM/

Slide 57

Slide 57 text

DDD and Patterns by Jimmy Nilsson http://amzn.com/B0054KOKQQ/

Slide 58

Slide 58 text

Links http://dddcommunity.org/ http://williamdurand.fr/ http://welcometothebundle.com/ http://verraes.net/ http://jimmynilsson.com/blog/ http://www.martinfowler.com/ http://elephantintheroom.io/ -- podcast http://msdn.microsoft.com/en-us/magazine/dd419654.aspx http://www.martinfowler.com/bliki/AnemicDomainModel.html http://martinfowler.com/bliki/CQRS.html

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

https://joind.in/11576 Thanks!