Skip to Content

Domain-Driven Design 🎯

DDD focuses on modeling the business domain in software.

Key Elements

  • Application layer - contains business logic corresponding to use cases and coordinating interactions between the domain layer and the infrastructure layer
public class OrderUseCase { @Transactional public void createOrder(@Valid Order order) { // paymentService.validate(order); orderRepository.create(order); shippingContext.prepare(order); } }
  • Ubiquitous Language - Use the language of the business in the code
public Customer findProductByUPC(String upc){ // Find product by UPC }
  • Bounded Context - The model applies to a specific context
// I'm doing one thing "Shipping" but doing it well. public class ShippingContext { public void ship(Order order) { // Shipping logic } }
  • Value Objects - Immutable objects identified by attributes
// Reusable objects across entities record Money(BigDecimal amount, String currency) { }
  • Entities - Domain objects with unique identity
// Object having meaningful representation for the domain record Warehouse(String warehouseId, List<Inventory> inventories) { }
  • Aggregates - Cluster of domain objects that belong together
// Aggregate of entities required by a specific context record CustomerAggregate(Customer customer, List<Address> addresses, List<Order> orders) { }

Project structure

    • OrderController.java (Entrypoint)
    • OrderUseCase.java
      • Address.java
      • Customer.java
      • Order.java
      • OrderService.java
      • OrderRepository.java (Interface)
      • InMemoryRepository.java
        • RemoteOrderRepository.java
        • LocalOrderRepository.java
  • OrderRepository belongs in domain because it works with domain objects without knowledge of any database or storage

Conclusion

The goal is to deeply reflect the real business rules and processes in the software model. By using the language of the business, the model is aligned with business thinking.

DDD is about creating software that matches how the business works rather than just functionally achieving tasks. The domain model captures business entities and logic at the core of the system.

Sources

Last updated on