Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
DDD in PHP
on example of Symfony
cystbear
Symfony expert
MongoDB adept
Erlang fun
OSS doer
KNPer
https://twitter.com/1cdecoder
https://github.com/cystbear
...
Learning Pyramid
PHP? ORLY?
What this talk about?
About useful tool/lib?
About success story?
No! It’s about idea
Motivation!
MVC
Where to store business logic?
Model
View
Controller
Where to store business logic?
Model
View
Controller
Where to store business logic?
Model
View
Controller
Where to store business logic?
Model
View
Controller -- YEAH!
Welcome to
Fat Stupid Ugly Controllers
FSUC/FUC
http://blog.astrumfutura.com/2008/12/the-m-in-mvc-why-models-are-misunders...
Anemic (Domain) Model
http://www.martinfowler.com/bliki/AnemicDomainModel.html
http://habrahabr.ru/post/224879/
“In essenc...
Persistence Layer
Model
What is
Not MVC (phew!)
Request / Response Framework
HTTP Framework
http://fabien.potencier.org/article/49/what-is-symfony2
What about model,
persistence layer?
Meet Doctrine
http://www.doctrine-project.org/
SQL -- DBAL + ORM
MongoDB
CouchDB
OrientDB
PHPCR ODM
OXM
What is
Inversion of control
Service Locator
Dependency Injection Container
http://www.martinfowler.com/articles/injection...
Services
http://groovy.codehaus.org/
https://grails.org/
Single Class
With its Deps (min) set
Easy to Replace
Easy to Test...
Controller’s pray
https://twitter.com/ornicar
Get Request
Submit form if any
Call one Service method
Return Response
Rende...
Managers Managers Managers
http://blog.codinghorror.com/i-shall-call-it-somethingmanager/
http://stackoverflow.com/questio...
Real Pain
class BackendUserProgramsPossessionFormHandler
{
protected $dep1; // deps holder props
public function __constru...
How Kris writes Symfony apps#44
https://twitter.com/kriswallsmith
http://www.slideshare.net/kriswallsmith/how-kris-writess...
https://twitter.com/kriswallsmith
http://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps
How Kris writes Symfo...
Domain Logic Patterns
http://martinfowler.com/books/eaa.html
Domain Logic Patterns
http://martinfowler.com/books/eaa.html
Transaction Script
Domain Model
Table Module
Service Layer
Transaction Script
Domain Model
Table Module
Domain Logic
&
Application logic
Service Layer
What is next?
RAD
DDD Patterns
Examples
Layers
Goodies
DDD != RAD
Code First
Do not Care about persistence (yet)
Domain Model
Repository
Value Object
DTO
Strategy
State
Patterns & Code
Domain Model
Domain Model
<?php
namespace MegaCorpCoreProduct;
class Product
{
private $id;
private $name;
private $recognitionStrategy...
Repository
<?php
namespace MegaCorpCoreProduct;
interface ProductRepository
{
public function find(ProductId $productId);
...
Value Object
<?php
namespace MegaCorpCore;
class ProductId
{
private $value;
public function __construct($value)
{
$this->...
Value Object DateRange
Value Object DateRange
<?php
public function findByDateRange(
DateTime $from, DateTime $to )
class DateRange
{
private $fr...
Value Object Money
<?php
class Money
{
private $amount;
private $currency;
public function __construct($amount, Currency $currency)
{
// ...
...
<?php
class ProfileData
{
public $firstName;
public $lastName;
public $birthday;
}
DTO
Strategy
______ ______ _______ _______
/ | / __  |  | ____|
| ,----'| | | | | .--. || |__
| | | | | | | | | || __|
| `----.| `--' |...
src
└── MegaCorp
├── ApiBundle
│ ├── Controller
│ │ └── ...
│ └── MegaCorpApiBundle.php
├── Core
│ └── Product
│ ├── Produ...
Layers
Layers
Domain Layer -- heart of your application, Entities and Repositories
Application Layer -- Controllers
Presentation ...
Useful goodies
BBB DDD by Eric Evans
http://amzn.com/0321125215/
DDD Quickly by InfoQ
http://www.infoq.com/minibooks/domain-driven-design-quickly
PoEAA by Martin Fowler
http://amzn.com/B008OHVDFM/
DDD and Patternsby Jimmy Nilsson
http://amzn.com/B0054KOKQQ/
Links
http://dddcommunity.org/
http://williamdurand.fr/
http://welcometothebundle.com/
http://verraes.net/
http://jimmynil...
https://joind.in/11576
Thanks!
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
Upcoming SlideShare
Loading in …5
×

DDD on example of Symfony (Webcamp Odessa 2014)

652 views

Published on

Published in: Engineering

DDD on example of Symfony (Webcamp Odessa 2014)

  1. 1. DDD in PHP on example of Symfony
  2. 2. cystbear Symfony expert MongoDB adept Erlang fun OSS doer KNPer https://twitter.com/1cdecoder https://github.com/cystbear http://knplabs.com/
  3. 3. Learning Pyramid
  4. 4. PHP? ORLY?
  5. 5. What this talk about?
  6. 6. About useful tool/lib?
  7. 7. About success story?
  8. 8. No! It’s about idea Motivation!
  9. 9. MVC
  10. 10. Where to store business logic? Model View Controller
  11. 11. Where to store business logic? Model View Controller
  12. 12. Where to store business logic? Model View Controller
  13. 13. Where to store business logic? Model View Controller -- YEAH!
  14. 14. 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/
  15. 15. 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
  16. 16. Persistence Layer Model
  17. 17. What is Not MVC (phew!) Request / Response Framework HTTP Framework http://fabien.potencier.org/article/49/what-is-symfony2
  18. 18. What about model, persistence layer?
  19. 19. Meet Doctrine http://www.doctrine-project.org/ SQL -- DBAL + ORM MongoDB CouchDB OrientDB PHPCR ODM OXM
  20. 20. 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
  21. 21. Services http://groovy.codehaus.org/ https://grails.org/ Single Class With its Deps (min) set Easy to Replace Easy to Test MVC(S)!
  22. 22. Controller’s pray https://twitter.com/ornicar Get Request Submit form if any Call one Service method Return Response Rendering HTML far away
  23. 23. 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 <SmtManager> <WhatEverManager> <MyManager> <EtcManager>
  24. 24. 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); // ... }
  25. 25. How Kris writes Symfony apps#44 https://twitter.com/kriswallsmith http://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps
  26. 26. https://twitter.com/kriswallsmith http://www.slideshare.net/kriswallsmith/how-kris-writessymfonyapps How Kris writes Symfony apps#44
  27. 27. Domain Logic Patterns http://martinfowler.com/books/eaa.html
  28. 28. Domain Logic Patterns http://martinfowler.com/books/eaa.html Transaction Script Domain Model Table Module Service Layer
  29. 29. Transaction Script
  30. 30. Domain Model
  31. 31. Table Module
  32. 32. Domain Logic & Application logic
  33. 33. Service Layer
  34. 34. What is next? RAD DDD Patterns Examples Layers Goodies
  35. 35. DDD != RAD Code First Do not Care about persistence (yet)
  36. 36. Domain Model Repository Value Object DTO Strategy State Patterns & Code
  37. 37. Domain Model
  38. 38. Domain Model <?php namespace MegaCorpCoreProduct; class Product { private $id; private $name; private $recognitionStrategy; public function __construct( ProductId $id, $name, $recognitionStrategy ) { $this->id = $id; $this->name = $name; $this->recognitionStrategy = $recognitionStrategy; }
  39. 39. Repository <?php namespace MegaCorpCoreProduct; interface ProductRepository { public function find(ProductId $productId); public function findAll(); public function add(Product $product); public function remove(Product $product); }
  40. 40. Value Object <?php namespace MegaCorpCore; class ProductId { private $value; public function __construct($value) { $this->value = (string) $value; } public function getValue() { return $this->value; } }
  41. 41. Value Object DateRange
  42. 42. 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)
  43. 43. Value Object Money
  44. 44. <?php class Money { private $amount; private $currency; public function __construct($amount, Currency $currency) { // ... } } Value Object Money
  45. 45. <?php class ProfileData { public $firstName; public $lastName; public $birthday; } DTO
  46. 46. Strategy
  47. 47. ______ ______ _______ _______ / | / __ | | ____| | ,----'| | | | | .--. || |__ | | | | | | | | | || __| | `----.| `--' | | '--' || |____ ______| ______/ |_______/ |_______|
  48. 48. 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
  49. 49. Layers
  50. 50. 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
  51. 51. Useful goodies
  52. 52. BBB DDD by Eric Evans http://amzn.com/0321125215/
  53. 53. DDD Quickly by InfoQ http://www.infoq.com/minibooks/domain-driven-design-quickly
  54. 54. PoEAA by Martin Fowler http://amzn.com/B008OHVDFM/
  55. 55. DDD and Patternsby Jimmy Nilsson http://amzn.com/B0054KOKQQ/
  56. 56. 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
  57. 57. https://joind.in/11576 Thanks!

×