Review Example for Mapping and Layering in a DDD-based Layered Architecture?

I struggle with principled DDD in a layered (controller, service, repository) application. When trying to seek answers online, I seem to get a lot of conflicting information/opinions. See my example below:

Domain

public class Customer {
    private Long id;
    private String name;
}

public class CustomerOrder {
    private Long id;
    private String productName;
    private int quantity;
    private Customer customer;
}

Entity

@Entity
public class CustomerEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
}

@Entity
public class CustomerOrderEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String productName;
    private int quantity;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_id")
    private CustomerEntity customer;
}

DTO

@Data
public class CustomerOrderRequestDTO {
    private Long customerId;
    private String productName;
    private int quantity;
}

@Data
public class CustomerOrderResponseDTO {
    private Long orderId;
    private String productName;
    private int quantity;
    private Long customerId;
    private String customerName;
}

Controller layer

@RestController
@RequestMapping("/api/orders")
@AllArgsConstructor
public class CustomerOrderController {

    private final CustomerOrderService customerOrderService;

    @PostMapping
    public ResponseEntity<CustomerOrderResponseDTO> createOrder(@RequestBody CustomerOrderRequestDTO requestDTO) {
        CustomerOrderResponseDTO responseDTO = customerOrderService.createOrder(requestDTO);
        return ResponseEntity.ok(responseDTO);
    }
}

Service layer

@Service
@AllArgsConstructor
public class CustomerOrderService {

    private final CustomerRepository customerRepository;
    private final CustomerOrderRepository customerOrderRepository;
    private final Mapper mapper;

     public CustomerOrderResponseDTO createOrder(CustomerOrderRequestDTO requestDTO) {
        
        CustomerEntity customerEntity = customerRepository.findById(requestDTO.getCustomerId())
                .orElseThrow(() -> new IllegalArgumentException("Customer not found"));

        Customer customer = mapper.map(customerEntity);
        CustomerOrder customerOrder = mapper.map(requestDTO, customer);

        //maybe do some validation on the domain instances

        CustomerOrderEntity customerOrderEntity = mapper.map(customerOrder);
        customerOrderRepository.save(customerOrderEntity);

        CustomerOrderResponseDTO responseDTO = mapper.map(customerOrder);

        return responseDTO;
    }
}

Repository layer

Just some default JpaRepostories.

Questions

Specific questions:

  • Should we get the Customer the customerRepository + mapping, or should we retrieve it using a CustomerService? If so, shouldn’t that CustomerService return a DTO instead of a domain object?
  • The service layer currently performs multiple mappings between entities, domain objects, and DTOs. Is this approach aligned with DDD principles, or should the mapping be done elsewhere (e.g., in the repository or controller)?
  • When creating the CustomerOrderResponseDTO, should I map from the CustomerOrderEntity or from the CustomerOrder?

General question:

  • Is the example above a principled design? If not, what would you change?

New contributor

Gman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật