Slide 1

Slide 1 text

Sergio Moya @soyelsergillo IN

Slide 2

Slide 2 text

@SOYELSERGILLO GITHUB.COM/SMOYA Backend Engineer at Social Point ! DDD Fan ! Hip Hop Culture lover ! Badaloní (from Badalona) SERGIO

Slide 3

Slide 3 text

WHAT

Slide 4

Slide 4 text

MODEL 3 2 4 1 DOMAIN CONTEXT UBIQUITOUS

Slide 5

Slide 5 text

UBIQUITOUS

Slide 6

Slide 6 text

KNOW YOUR UBIQUITOUS LANGUAGE

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

THE

Slide 10

Slide 10 text

OUR

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

GENERATING

Slide 13

Slide 13 text

THE

Slide 14

Slide 14 text

THE

Slide 15

Slide 15 text

AND

Slide 16

Slide 16 text

THE

Slide 17

Slide 17 text

SERVICE ORM CONTRO LLER REPOSIT ORY DB MANAGER YOUR

Slide 18

Slide 18 text

THIS IS WAR!

Slide 19

Slide 19 text

BUT YOU WANT THIS

Slide 20

Slide 20 text

BUT WE WANT THIS TODAY YOU WILL GET THIS

Slide 21

Slide 21 text

LET’S

Slide 22

Slide 22 text

…⋯ SRC …⋯ …⋯ PAYMENTBUNDLE …⋯ COMPONENT PAYMENT BUNDLE FILE

Slide 23

Slide 23 text

OUR

Slide 24

Slide 24 text

OUR

Slide 25

Slide 25 text

OUR

Slide 26

Slide 26 text

We are recording Events!

Slide 27

Slide 27 text

BUT WHAT IS A DOMAIN EVENT?

Slide 28

Slide 28 text

“CAPTURES

Slide 29

Slide 29 text

DOMAIN

Slide 30

Slide 30 text

DOMAIN

Slide 31

Slide 31 text

DOMAIN

Slide 32

Slide 32 text

DOMAIN

Slide 33

Slide 33 text

REMEMBER A CUSTOMER CAN ORDER A PRODUCT ! A CUSTOMER CAN PAY AN ORDER

Slide 34

Slide 34 text

OUR

Slide 35

Slide 35 text

OUR

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

WHAT

Slide 38

Slide 38 text

WHAT

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

OK,

Slide 41

Slide 41 text

OK,

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

LET’S

Slide 46

Slide 46 text

TAKE A PIECE OF CQRS COMMAND QUERY RESPONSIBILITY SEGREGATION

Slide 47

Slide 47 text

Any method that mutates state. Any method that returns a value. COMMAND QUERY COMMAND

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

COMPONENT

Slide 51

Slide 51 text

WE

Slide 52

Slide 52 text

IMAGINE

Slide 53

Slide 53 text

FILE

Slide 54

Slide 54 text

WE

Slide 55

Slide 55 text

OUR

Slide 56

Slide 56 text

CONSTRUCT

Slide 57

Slide 57 text

INFRASTRUCTURE

Slide 58

Slide 58 text

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!

Slide 59

Slide 59 text

AND

Slide 60

Slide 60 text

AND

Slide 61

Slide 61 text

PAYMENT

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

OUR

Slide 64

Slide 64 text

BE HEXAGONAL!

Slide 65

Slide 65 text

ENJOY DDD

Slide 66

Slide 66 text

LINKS

Slide 67

Slide 67 text

QUESTIONS?

Slide 68

Slide 68 text

Sergio Moya @soyelsergillo THANKS I’M