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 on example of Symfony (SfCampUA14)

4,336 views

Published on

DDD on example of Symfony (SfCampUA14)

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

×