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

DDD on example of Symfony (SymfonyCamp UA 2014)

DDD on example of Symfony (SymfonyCamp UA 2014)

How I realized that MVC is a myth and why MVC duped us for a years.

Oleg Zinchenko

November 02, 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. Controller’s pray https://twitter.com/ornicar Get Request Submit form if any Call

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

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

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

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