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

Implementing DDD in your project

Implementing DDD in your project

This presentation shows a simple approach of a DDD implementation in your project.
Was exposed in PHP Barcelona first Monthly Talk (Barcelona)

Sergio Moya

June 11, 2014
Tweet

More Decks by Sergio Moya

Other Decks in Technology

Transcript

  1. @SOYELSERGILLO GITHUB.COM/SMOYA Backend Engineer at Social Point ! DDD Fan

    ! Hip Hop Culture lover ! Badaloní (from Badalona) SERGIO
  2. The Lead of the Backend engineers team has decided to

    split the team into smaller two-member teams. ! Each team will develop a different component. ! Our team will develop the Payment Component
  3. THE

  4. OUR

  5. THE

  6. THE

  7. AND

  8. THE

  9. OUR

  10. OUR

  11. OUR

  12. OUR

  13. OUR

  14. public function pay(Money $money)! {! try {! if (!$this->product->getPrice()->isEqualTo($money)) {!

    throw new OrderException('The Order amount is not satisfied');! }! $this->product->deliver($this->customer);! $this->status = OrderStatus::create(OrderStatus::ORDER_PAID);! } catch (\Exception $e) {! $this->status = OrderStatus::create(OrderStatus::ORDER_FAILED);! }! return $this->status;! } OUR
  15. interface OrderRepository! {! public function exists($id);! ! public function find($id,

    $customerId);! ! public function findByCustomerId($customerId);! ! public function save($id, $productId, $customerId, $status = 1);! ! public function remove($id);! ! public function removeByCustomerId($customerId);! ! public function clearAll();! }! OUR
  16. OK,

  17. OK,

  18. public function buyProductAction(Request $request)! {! $customer = new Customer($request->request->get('customerId'));! !

    $productRepository = $this->get('tato.payment.product_respository');! $product = $productRepository->find($request->request->get('productId'));! ! $order = $customer->order($product);! ! $paid = new Money(! $request->request->get('paid'), ! $request->request->get(‘currency')! );! $status = $order->pay($paid);! ! $this->get(‘tato.payment.order_repository')->save(! ! ! $order->getId(), ! $product, ! $customer->getId(), ! $status->getStatus()! );! ! return new JsonResponse(array('status' => $status->getStatus()));! }! } A
  19. public function buyProductAction(Request $request)! {! $customer = new Customer($request->request->get('customerId'));! !

    $productRepository = $this->get('tato.payment.product_respository');! $product = $productRepository->find($request->request->get('productId'));! ! $order = $customer->order($product);! ! $paid = new Money(! $request->request->get('paid'), ! $request->request->get(‘currency')! );! $status = $order->pay($paid);! ! $this->get(‘tato.payment.order_repository')->save(! ! ! $order->getId(), ! $product, ! $customer->getId(), ! $status->getStatus()! );! ! return new JsonResponse(array('status' => $status->getStatus()));! }! } A
  20. class BuyProductCommand! {! public $customerId;! ! public $productId;! ! public

    $paid;! ! public $currency;! ! public function __construct($customerId, $productId, $paid, $currency)! {! $this->customerId = $customerId;! $this->productId = $productId;! $this->paid = $paid;! $this->currency = $currency;! } ! } THE
  21. public function handle(BuyProductCommand $command)! {! $customer = new Customer($command->customerId);! $product

    = $this->productRepository->find($command->productId);! ! $order = $customer->order($product);! $status = $order->pay(new Money($command->paid, $command->currency));! ! $events = $customer->getRecordedEvents();! ! // @todo do something with this events! ! $this->orderRepository->setOrder(! $command->gatewayName,! $order->getId(),! $command->customerId,! $status->getStatus()! );! ! return $order;! } A
  22. WE

  23. WE

  24. OUR

  25. SERVICES services:! tato.payment.order_repository:! class: Tato\Component\Game\CustomOrderRepository! arguments:! - @doctrine.orm.entity_manager! ! tato.payment.product_repository:!

    class: Tato\Component\Game\Product\CustomProductRepository! arguments:! - @doctrine.orm.entity_manager! ! tato.payment.command.handler.buy_product:! class: Tato\Component\Payment\Command\BuyProductCommandHandler! arguments:! order_repository: @tato.payment.order_repository! product_repository: @tato.payment.product_repository!
  26. AND

  27. AND

  28. public function buyProductAction(Request $request)! {! $customer = new Customer($request->request->get('customerId'));! !

    $productRepository = $this->get('tato.payment.product_respository');! $product = $productRepository->find($request->request->get('productId'));! ! $order = $customer->order($product);! ! $paid = new Money(! $request->request->get('paid'), ! $request->request->get(‘currency')! );! $status = $order->pay($paid);! ! $this->get(‘tato.payment.order_repository')->save(! ! ! $order->getId(), ! $product, ! $customer->getId(), ! $status->getStatus()! );! ! return new JsonResponse(array('status' => $status->getStatus()));! }! } THE
  29. OUR