Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DDD in PHP, on example of Symfony

DDD in PHP, on example of Symfony

Introduction to DDD on example of Symfony

Oleg Zinchenko

July 24, 2014
Tweet

More Decks by Oleg Zinchenko

Other Decks in Programming

Transcript

  1. cystbear Symfony expert MongoDB adept Erlang fun OSS doer KNPer

    https://twitter.com/1cdecoder https://github.com/cystbear http://knplabs.com/
  2. MVC

  3. 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
  4. What is Not MVC (phew!) Request / Response Framework HTTP

    Framework http://fabien.potencier.org/article/49/what-is-symfony2
  5. 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
  6. Controller’s pray https://twitter.com/ornicar Get Request Submit form if any Call

    one Service method Return Response Rendering HTML far away
  7. 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); // ... }
  8. 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; }
  9. 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); }
  10. Value Object <?php namespace MegaCorp\Core; class ProductId { private $value;

    public function __construct($value) { $this->value = (string) $value; } public function getValue() { return $this->value; } }
  11. 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)
  12. <?php class Money { private $amount; private $currency; public function

    __construct($amount, Currency $currency) { // ... } } Value Object Money
  13. ______ ______ _______ _______ / | / __ \ |

    \ | ____| | ,----'| | | | | .--. || |__ | | | | | | | | | || __| | `----.| `--' | | '--' || |____ \______| \______/ |_______/ |_______|
  14. 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
  15. 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
  16. 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