11. Past
● CRUD doesn’t make sense anymore
● It had sense at the beginning
● Product, Logistic, Delivery, Cart, Customers, ...
● It’s not sustainable.
Decoupling Ulabox.com monolith. From CRUD to DDD
12. Our solution
/**
* @param Cart $cart
*/
public function createOrder(Cart $cart)
{
$createOrder = new CreateOrderCommand(Cart $cart);
$this->commandBus->dispatch($createOrder)
}
Event bus
CreateOrder command
OrderWasCreated event
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
subscribe
Decoupling Ulabox.com monolith. From CRUD to DDD
20. class OrderController extends BaseController
{
public function refundAction(Request $request, $id)
{
$em = $this->container->get('doctrine.orm.entity_manager');
$orderPayment = $em->getRepository('UlaboxCoreBundle:OrderPayment')->find($id);
$amount = $request->request->get('refund');
$data = $this->container->get('sermepa')->processRefund($orderPayment, $amount);
$orderRefund = new OrderPayment();
$orderRefund->setAmount($amount);
...
$em->persist($orderRefund);
$em->flush();
return $this->redirectToRoute('order_show', ['id' => $orderPayment->getOrder()->getId()]);
}
public function someOtherAction(Request $request, $id)
...
}
21. Decoupling Ulabox.com monolith. From CRUD to DDD
Problems
● Hidden dependencies
● Inheritance.
● Biz logic in the controller.
● Non aggregate root.
● Difficult to test.
22. * Dependency Injection: https://es.wikipedia.org/wiki/Inyecci%C3%B3n_de_dependencias
Decoupling Ulabox.com monolith. From CRUD to DDD
Solutions
● Dependency Injection
● Break inheritance from base controller.
● Application services.
● Testing
39. class Reschedule
{
public $orderId;
public $addressId;
public $slotVars;
public $comments;
public function __construct($orderId, $addressId, $slotVars, $comments)
{
$this->orderId = $orderId;
$this->addressId = $addressId;
$this->slotVars = $slotVars;
$this->comments = $comments;
}
}
50. class RescheduleHandler extends CommandHandler
{
public function __construct( ... ) { ... }
public function handleReschedule(Reschedule $rescheduleOrder)
{
$timeLineSlot = $this->slotManager->createTimelineSlotFromVars($rescheduleOrder->slotVars);
$order = $this->orderRepository->get($rescheduleOrder->aggregateId);
$delivery = $order->getOrderDelivery();
$delivery->setSlot($timeLineSlot->getSlot());
$delivery->setLoadTime($timeLineSlot->getLoadTime());
$delivery->setShift($timeLineSlot->getShift()->getShift());
...
$order->rescheduleDelivery($delivery);
$this->orderRepository->save($order);
$this->eventBus->publish($order->getUncommittedEvents());
}
}
51. Decoupling Ulabox.com monolith. From CRUD to DDD
Problems
● Biz logic out of domain.
● Aggregate access.
● Aggregate Root?
● Unprotected Domain.
52. Decoupling Ulabox.com monolith. From CRUD to DDD
Solutions
● Aggregate Root. Order or Delivery?
● Unique acces point to the domain.
● Clear intention!!
● Testing.
72. class RegisterCreditCard
{
public $customerId;
public $cardNumber;
public $type;
public $token;
public $expiry;
public function __construct($customerId, $cardNumber, $type, $token, $expiry)
{
$this->customerId = $customerId;
$this->cardNumber = $cardNumber;
$this->type = $type;
$this->token = $token;
$this->expiry = $expiry;
}
}