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.

Pursuing practices of Domain-Driven Design in PHP

5,964 views

Published on

Published in: Technology
  • Be the first to comment

Pursuing practices of Domain-Driven Design in PHP

  1. 1. Pursuing practices ofDomain-Driven Design in PHP
  2. 2. Who am I Giorgio SironiBachelor in Computer Engineering Advisor @ AllbusZone Leader @ DZone
  3. 3. The long title Pursuing practices of Domain-Driven Design in PHP
  4. 4. The DDD box of goodsKnowledge crunchingUbiquitous LanguageDomain ModelRefactoring towards deeperinsight...Collaboration patterns (Anti-corruption layer, Separateways, ...)
  5. 5. Why? (the most important slide) Close to business, to follow its changes aids iterative development The code is the design supported by blackboards and UML Test and develop with in-memory objects no instantiation of Oracle connections Its also fun! exercise creativity and learning skills
  6. 6. Domain ModelReflects knowledge of the domain more thantechnology While there is value in the item on the right, we value the item on the left more Persistence-agnostic (UnitOfWork) In general, no outward dependencies
  7. 7. Step 1: from relational...user groupid idusername namepassword logoactive user_group id_user id_group
  8. 8. Step 1: ...to object-orientedclass User class Group{ {} }
  9. 9. Step 2: from Active Record... class Group extends Doctrine_Record { } Hard dependency towards ORM Inherits pollution from Doctrine_Record
  10. 10. Step 2: … to Data Mapper/** * @Entity */class User{}
  11. 11. Building blocksEntityValue ObjectAggregateRepositoryFactoryServiceOther transient objects (Specification,Parameter Objects, ...)
  12. 12. EntityMore than a rowEquality is based on identity e.g. Post #42, user giorgiosironi, …The bread and butter of your Domain Model
  13. 13. Value ObjectPreviously known in the world as value e.g. the number 42, Zip code 22031, #FF0000Equality based on valuesImmutable in certain implementations
  14. 14. AggregateSubgraph of Entities and Value Objects with a single entry: the root possible multiple exits, mostly to follow during readingUnit of consistency: loose correspondency todatabase transactionsPartitions the state of the application
  15. 15. Checkpoint: data modeling
  16. 16. RepositoryThe gate to the database One aggregate at the timeThe illusion of an in-memory collection ofEntities Fowlers definition Heres a BookRepository
  17. 17. FactoryEncapsulate creation of complex Aggregates a new() is often all you need
  18. 18. ServiceAt the domain levelTo help Entities and Value Objects to avoid mutual dependencies or field references to strangers optimal for isolation from libraries
  19. 19. Checkpoint: lifecycle classes
  20. 20. Meanwhile, in PHP...
  21. 21. On frameworksOnly one suggestion: build your Domain Model like if the framework didnt exist
  22. 22. Active Record vs. Data MapperDoctrine 2 for persistence (see tutorial by@juokaz)By default for Doctrine 2 object === rowAll the tricks are athttp://github.com/giorgiosironi/ddd-talk
  23. 23. Entity@Entity annotation no extends@Column for fields private fields types are PHP types: string, not varchar
  24. 24. Value ObjectsDo you want an (id, date) table? Serialization of the whole object Conversion into a custom string/numerical format via a custom DBAL data type Deconstruction/reconstruction with lifecycle hooks Combined approaches: serialization and mirror fields
  25. 25. Aggregate Relationships: @OneToMany, @ManyToMany, @OneToOne, …● @OneToMany(..., cascade={“persist”, “remove”})● @OneToMany(..., orphanRemoval=true)● @ManyToMany(targetEntity="Phonenumber") @JoinTable(..., inverseJoinColumns={name=”phonenumber_id", referencedColumnName="id", unique=true)
  26. 26. RepositoryPlain Old PHP ObjectComposing EntityManager Its possible to define EntityManager custom repositories: quick and dirtyEncapsulates queriesTypical methods: add($root), remove($root),find($id), findByStrangeCriteria() Its a collection!
  27. 27. Factory, ServicePOPO Sometimes composing services or infrastructure objects (e.g. generating new progressive number for invoices, calculate current taxes, sending mails...)Often decoupled with an interface
  28. 28. ReferencesThe code shown in this talk http://github.com/giorgiosironi/ddd-talkFour books http://domaindrivendesign.org/booksDomain-Driven Design mailing list Google that :)
  29. 29. Q/A
  30. 30. FeedbackTesting in isolation tutorial: http://joind.in/3216 DDD talk: http://joind.in/3224
  31. 31. Thanks for your attention

×