vendredi 9 septembre 2022

DDD, should I create a domain service just to save data?

I'm trying to folllow this guide https://www.baeldung.com/hexagonal-architecture-ddd-spring . After reading along I found this interesting approach

Lastly, we should make sure that the Order will be always saved after each action. To do that, we'll define a Domain Service, which usually contains logic that can't be a part of our root:

So from what I understand a domain service is what we use when we can't fit business logic in a single domain object. But what the guide did was just saving a single domain object to the database because we can't have the repository within our domain object right??. If that's the case why don't we just let the controller have access to the repository (some say it's bad practice some say "no" I don't even understand??) since we already put business logic in the domain object. so we would have something like this.

@PostMapping
public Mono<Order> addProduct(Request request) {
    return orderRepository.findById(request.orderId())
                          .map(order -> {
                              order.addProduct(reqeust.getProduct());
                              return order;
                          })
                          .flatMap(order-> orderRepository.save(order));
}.

Should we create an additional layer and call it Domain Service for saving data ? please help me understand best practice.

Aucun commentaire:

Enregistrer un commentaire