of 60 /60
DDD in PHP on example of Symfony

Web campodessa zinchenko v2 (23-07-2014)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Web campodessa zinchenko v2 (23-07-2014)

DDD in PHPon example of Symfony

Page 2: Web campodessa zinchenko v2 (23-07-2014)

cystbearSymfony expert

MongoDB adept

Erlang fun

OSS doer

KNPer

https://twitter.com/1cdecoderhttps://github.com/cystbearhttp://knplabs.com/

Page 3: Web campodessa zinchenko v2 (23-07-2014)

Learning Pyramid

Page 4: Web campodessa zinchenko v2 (23-07-2014)

PHP? ORLY?

Page 5: Web campodessa zinchenko v2 (23-07-2014)

What this talk about?

Page 6: Web campodessa zinchenko v2 (23-07-2014)

About useful tool/lib?

Page 7: Web campodessa zinchenko v2 (23-07-2014)

About success story?

Page 8: Web campodessa zinchenko v2 (23-07-2014)

No! It’s about idea

Motivation!

Page 9: Web campodessa zinchenko v2 (23-07-2014)

MVC

Page 10: Web campodessa zinchenko v2 (23-07-2014)

Where to store business logic?

ModelViewController

Page 11: Web campodessa zinchenko v2 (23-07-2014)

Where to store business logic?

ModelViewController

Page 12: Web campodessa zinchenko v2 (23-07-2014)

Where to store business logic?

ModelViewController

Page 13: Web campodessa zinchenko v2 (23-07-2014)

Where to store business logic?

ModelViewController -- YEAH!

Page 14: Web campodessa zinchenko v2 (23-07-2014)

Welcome to

Fat Stupid Ugly ControllersFSUC/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/

Page 15: Web campodessa zinchenko v2 (23-07-2014)

Anemic (Domain) Model

http://www.martinfowler.com/bliki/AnemicDomainModel.htmlhttp://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

Page 16: Web campodessa zinchenko v2 (23-07-2014)

Persistence Layer

Model

Page 17: Web campodessa zinchenko v2 (23-07-2014)
Page 18: Web campodessa zinchenko v2 (23-07-2014)

What is

Not MVC (phew!)

Request / Response FrameworkHTTP Framework

http://fabien.potencier.org/article/49/what-is-symfony2

Page 19: Web campodessa zinchenko v2 (23-07-2014)

What about model,persistence layer?

Page 20: Web campodessa zinchenko v2 (23-07-2014)

Meet Doctrine

http://www.doctrine-project.org/

SQL -- DBAL + ORMMongoDBCouchDBOrientDBPHPCR ODMOXM

Page 21: Web campodessa zinchenko v2 (23-07-2014)

What is

Inversion of controlService LocatorDependency Injection Container

http://www.martinfowler.com/articles/injection.htmlhttp://fabien.potencier.org/article/11/what-is-dependency-injection

Page 22: Web campodessa zinchenko v2 (23-07-2014)

Services

http://groovy.codehaus.org/https://grails.org/

Single ClassWith its Deps (min) setEasy to ReplaceEasy to Test

MVC(S)!

Page 23: Web campodessa zinchenko v2 (23-07-2014)

Controller’s pray

https://twitter.com/ornicar

Get RequestSubmit form if anyCall one Service methodReturn Response

Rendering HTML far away

Page 25: Web campodessa zinchenko v2 (23-07-2014)

Real Painclass BackendUserProgramsPossessionFormHandler{ protected $dep1; // deps holder props

public function __construct(DepsClass $dep1 /*, ...*/) { $this->dep1 = $dep1; }

public function process(Form $form) { $this->dep1->makeHappy($form); // ... }

Page 26: Web campodessa zinchenko v2 (23-07-2014)

How Kris writes Symfony apps#44

https://twitter.com/kriswallsmithhttp://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps

Page 27: Web campodessa zinchenko v2 (23-07-2014)

https://twitter.com/kriswallsmithhttp://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps

How Kris writes Symfony apps#44

Page 28: Web campodessa zinchenko v2 (23-07-2014)

Domain Logic Patterns

http://martinfowler.com/books/eaa.html

Page 29: Web campodessa zinchenko v2 (23-07-2014)

Domain Logic Patterns

http://martinfowler.com/books/eaa.html

Transaction ScriptDomain ModelTable ModuleService Layer

Page 30: Web campodessa zinchenko v2 (23-07-2014)

Transaction Script

Page 31: Web campodessa zinchenko v2 (23-07-2014)

Domain Model

Page 32: Web campodessa zinchenko v2 (23-07-2014)

Table Module

Page 33: Web campodessa zinchenko v2 (23-07-2014)

Domain Logic&

Application logic

Page 34: Web campodessa zinchenko v2 (23-07-2014)

Service Layer

Page 35: Web campodessa zinchenko v2 (23-07-2014)
Page 36: Web campodessa zinchenko v2 (23-07-2014)

What is next?RADDDD PatternsExamplesLayersGoodies

Page 37: Web campodessa zinchenko v2 (23-07-2014)

DDD != RADCode FirstDo not Care about persistence (yet)

Page 38: Web campodessa zinchenko v2 (23-07-2014)

Domain ModelRepositoryValue ObjectDTOStrategyState

Patterns & Code

Page 39: Web campodessa zinchenko v2 (23-07-2014)

Domain Model

Page 40: Web campodessa zinchenko v2 (23-07-2014)

Domain Model<?php

namespace MegaCorp\Core\Product;

class Product{ private $id; private $name; private $recognitionStrategy;

public function __construct( ProductId $id, $name, $recognitionStrategy ) { $this->id = $id; $this->name = $name; $this->recognitionStrategy = $recognitionStrategy; }

Page 41: Web campodessa zinchenko v2 (23-07-2014)

Repository<?php

namespace MegaCorp\Core\Product;

interface ProductRepository{ public function find(ProductId $productId);

public function findAll();

public function add(Product $product);

public function remove(Product $product);}

Page 42: Web campodessa zinchenko v2 (23-07-2014)

Value Object<?php

namespace MegaCorp\Core;

class ProductId{ private $value;

public function __construct($value) { $this->value = (string) $value; }

public function getValue() { return $this->value; }}

Page 43: Web campodessa zinchenko v2 (23-07-2014)

Value Object DateRange

Page 44: Web campodessa zinchenko v2 (23-07-2014)

Value Object DateRange<?php

public function findByDateRange( \DateTime $from, \DateTime $to )

class DateRange{ private $from; private $to; public function __construct(\DateTime $from, \DateTime $to) { $this->from = $from; $this->to = $to; }}

public function findByDateRange(\DateRange $range)

Page 45: Web campodessa zinchenko v2 (23-07-2014)

Value Object Money

Page 46: Web campodessa zinchenko v2 (23-07-2014)

<?php

class Money{ private $amount; private $currency;

public function __construct($amount, Currency $currency) { // ... }}

Value Object Money

Page 47: Web campodessa zinchenko v2 (23-07-2014)

<?php

class ProfileData{ public $firstName; public $lastName; public $birthday;}

DTO

Page 48: Web campodessa zinchenko v2 (23-07-2014)

Strategy

Page 49: Web campodessa zinchenko v2 (23-07-2014)

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

Page 50: Web campodessa zinchenko v2 (23-07-2014)

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

Page 51: Web campodessa zinchenko v2 (23-07-2014)

Layers

Page 52: Web campodessa zinchenko v2 (23-07-2014)

LayersDomain Layer -- heart of your application, Entities and Repositories

Application Layer -- ControllersPresentation Layer -- Templates / DTOs for serializerInfrastructure Layer -- framework, persistence, concrete implementations of Domain Layer

Page 53: Web campodessa zinchenko v2 (23-07-2014)

Useful goodies

Page 54: Web campodessa zinchenko v2 (23-07-2014)

BBB DDD by Eric Evans

http://amzn.com/0321125215/

Page 55: Web campodessa zinchenko v2 (23-07-2014)

DDD Quickly by InfoQ

http://www.infoq.com/minibooks/domain-driven-design-quickly

Page 56: Web campodessa zinchenko v2 (23-07-2014)

PoEAA by Martin Fowler

http://amzn.com/B008OHVDFM/

Page 57: Web campodessa zinchenko v2 (23-07-2014)

DDD and Patterns by Jimmy Nilsson

http://amzn.com/B0054KOKQQ/

Page 59: Web campodessa zinchenko v2 (23-07-2014)
Page 60: Web campodessa zinchenko v2 (23-07-2014)

https://joind.in/11576

Thanks!