The slides for a talk I gave for the Software Craftsmanship Barcelona 2014 about Domain-Driven Design (DDD) on the main Tactical Design Patterns with examples written in PHP
language, and an even smaller complement of objects normally used as values, you will make and use new objects that represent the meaningful quantities of your business”
public function __construct(Country $country, City $city, PostalCode $postalCode) { $this->setCountry($country); $this->setCity($city); $this->setPostalCode($postalCode); } public function country() { return $this->country; } public function city() { return $this->city; } public function postalCode() { return $this->postalCode; } // ... }
an specific Order $anOrder = Order::find(1); // Find all orders whose amount is greater than 100 EUR $orders = Order::where('amount', '>', 100)->take(10)->get(); // Create and save a new order $aNewOrder = Order::create(/* ... */); $aNewOrder->save();
$id;} public function getId(){return $this->id;} public function setCustomerId($customerId){$this->customerId = $customerId;} public function getCustomerId(){return $this->customerId;} public function setAmount($amount){$this->amount = $amount;} public function getAmount(){return $this->amount;} public function setStatus($status){$this->status = $status;} public function getStatus(){return $this->status;} public function setCreatedAt($createdAt){$this->createdAt = $createdAt;} public function getCreatedAt(){return $this->createdAt;} public function setUpdatedAt($updatedAt){$this->updatedAt = $updatedAt;} public function getUpdatedAt(){return $this->updatedAt;} }
// Update order status $anOrder->setStatus(Order::STATUS_ACCEPTED); // Update updatedAt field $anOrder->setUpdatedAt(new DateTimeImmutable()); // Save the order to the database $orderRepository->save($anOrder);
{ $this->orderRepository = $orderRepository; } public function execute($anOrderId, $anOrderStatus) { // fetch an order from the database $anOrder = $this->orderRepository->find($anOrderId); // Update order status $anOrder->setStatus($anOrderStatus); // Update updatedAt field $anOrder->setUpdatedAt(new DateTimeImmutable()); // Save the order to the database $this->orderRepository->save($anOrder); } }
{ $this->orderRepository = $orderRepository; } public function execute($anOrderId, $anOrderStatus) { // fetch an order from the database $anOrder = $this->orderRepository->find($anOrderId); // Update order status $anOrder->setStatus($anOrderStatus); // Update updatedAt field $anOrder->setUpdatedAt(new DateTimeImmutable()); // Save the order to the database $this->orderRepository->save($anOrder); } }
public function handle($anEvent) { $anOrder = $this->repository->find($anEvent->orderId()); $this->notifier->sendInvoiceFor($anOrder); } public function subscribedTo() { return OrderPaid::class; } }