O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Baby steps to Domain-Driven Design

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Carregando em…3
×

Confira estes a seguir

1 de 37 Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Quem viu também gostou (20)

Anúncio

Semelhante a Baby steps to Domain-Driven Design (20)

Anúncio

Mais recentes (20)

Baby steps to Domain-Driven Design

  1. 1. Domain-Driven Design Baby Steps Žilvinas Kuusas / Kaunas PHP v.28
  2. 2. How I found DDD? ● Project with complicated logic ● Complex business problems ● Implementation via experiments - domain modeling
  3. 3. When you need DDD ● Want to build long-lasting codebase ● Project contains lots of business logic ● You have a team ● Multiple teams working on a project
  4. 4. Domain-driven design ● Not a technology ● Not a methodology
  5. 5. Domain-driven design ● Structure of practices for making design decisions ● Focused on core domain and domain logic ● Technical and business people collaboration ● Ubiquitous language
  6. 6. Domain Concept of specific business area - real world problem.
  7. 7. Domain model Systematic code which solves problems described in domain in software level.
  8. 8. The same language used in domain model both by tech and business people to describe activities in domain. Ubiquitous language
  9. 9. Design pattern knowledge ● Dependency Injection ● Factory ● Data Mapper ● Adapter ● Mediator ● Command ● ...
  10. 10. Using a framework? Forget it for a while.
  11. 11. ● Modular structure ● Clear interface of object ● Messaging between objects Take advantage of OOP
  12. 12. Model-driven design Example
  13. 13. Where to start? ● Analyze domain problems ● Use same language in a team ● Distillate domain objects (mostly entities) ● Define domain events/actions ● Write code ● Repeat
  14. 14. Building blocks ● Entity ● Value object ● Repository ● Domain service ● Domain event ● Application service
  15. 15. Layered architecture
  16. 16. Domain ● Expenses tracker ○ Log incomes ○ Log outcomes ○ Tag transactions
  17. 17. Modeling ● Understand domain (notes, drawings, UML) ● Write behaviors (BDD) ● Write tests (TDD) ● Create classes
  18. 18. Entities Transaction ● created : DateTime ● amount : float ● tag : Tag ● type : string ● description : text Tag ● name : string
  19. 19. O/ Income || Transaction / Outcome Use case
  20. 20. Domain events and actions Events ● Transaction created Actions / use cases ● Create outcome transaction ● Create income transaction
  21. 21. My blocks ● Entities ○ Transaction ○ Tag ● Repositories ○ TransactionRepository ○ TagRepository ● Events ○ TransactionCreated ○ TransactionFailed ● Domain Services ○ OutcomeTransaction ○ IncomeTransaction ● Application Service ○ CreateTransaction
  22. 22. Core domain ● CreateOutcome CreateIncome ● Transaction TransactionRepositoryInterface Tag TagRepositoryInterface ● TransactionCreated TransactionFailed
  23. 23. ● TransactionController ● CreateTransaction ● TransactionRepository
  24. 24. class TransactionController { public function __construct( CreateTransaction $useCase, OutcomeTransaction $outcome ) { $this->useCase = $useCase; $this->outcome = $outcome; } public function createOutcomeAction() { $this->useCase->create($this->outcome->create(12.99)); } } Controller
  25. 25. Application Service class CreateTransaction { ... public function __construct( TransactionRepositoryInterface $repository, EventDispatcherInterface $dispatcher ) { $this->repository = $repository; $this->dispatcher = $dispatcher; } public function create(Transaction $transaction) { $this->repository->save($transaction); $event = new TransactionCreated($transaction); $this->dispatcher->dispatch($event::NAME, $event); } }
  26. 26. Domain Service class OutcomeTransaction { public function create($amount, Tag $tag, $description) { $transaction = new Transaction(); $transaction->setCreated(new DateTime()); $transaction->setAmount($amount); $transaction->setTag($tag); $transaction->setType($transaction::OUTCOME); $transaction->setDescription($description); return $transaction; } }
  27. 27. class TransactionRepository extends EntityRepository implements TransactionRepositoryInterface { public function save(Transaction $transaction) { $this->_em->persist($transaction); $this->_em->flush(); } } Repository
  28. 28. Repository Interface interface TransactionRepositoryInterface { public function save(Transaction $transaction); }
  29. 29. Domain model is always in valid state.
  30. 30. Domain isolation ● Isolate via use-cases ● Infrastructure connected via clear interfaces
  31. 31. Intention-revealing interfaces <?php interface TransactionRepository { public function getTaggedBy(Tag $tag); public function getByTimeframe( DateTime $from, DateTime $to ); } <?php interface TransactionRepository { public function getTagged($id); public function getByTimeframe( $from, $to ); }
  32. 32. Implementation ● Web controller, CLI ● Persistence / DB File system Web services Email sending
  33. 33. More complexity
  34. 34. Summary ● No need to think about framework, external tools ● Focus on business problems ● Explore them ● Solve them ● Build long-lasting implementation
  35. 35. Reality check ● Every domain is unique ● Don't force design principles ● Explore domain and how real things get done ● Development is iterative
  36. 36. The big blue book about DDD by Eric Evans
  37. 37. Ačiū! “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.“ Martin Fowler

×