0

According to Should I use a layer between service and repository for a clean architecture - Spring the Peristance layer is deprecated because the Repository is already an abstraction.

UML solution of the previous questionenter image description here

Product and ProductEntityJPA

But, in reality we have to use two entity :

  • One for the domain entity Product
  • One for the database entity ProductEntityJPA

The following UMl diagram represent the new solution. Please take note that the method save take now an ProductEntityJPA

enter image description here

How ProductService use ProductRepository ?

public ProductService {
    private ProductRepository productRepository;

    void registerNewProduct(Product product) {
        productRepository.save( /* give a ProductEntityJPA */);
    }
}

The ProductService class is aware of the JPA framework. We have therefore broken the rules that the Service layer is independent of any framework.

How to avoid dependencies between ProductService and ProductEntityJPA ?

Create a DTO

enter image description here

Inheritance

The ProductPersistanceDTO has to extend ProductEntityJPA because the method save in JPARepository is

public <S extends T> S save(S entity)

Solve the problem ?

So have you solve the problem, because by inheritance ProductService can access to the ProductEntityJPA ?

Create a new layer (Adapter pattern)

Come back to a solution with the persistance layer where the ProductGatewayImpl is an adapter

enter image description here

public ProductGatewayImpl {
    private ProductRepository productRepository;

    void save(Product product) {
        productRepository.save(  transform(product) );
    }

    private ProductEntityJPA transform(Product product) {
        /* transform */
        return productEntityJPA;
    }
}

What do you think

  • Do you think that the Persistance layer is useless (url link) ?
  • Do you think that the DTO is an solution (be aware of the inheritance) ?
  • Do you think that the Peristance layer (gateway) is an solution ?
2
  • Hi Adri! Welcome to SE. I'm afraid to say that I have voted to close the question. I have found several reasons to do so. 1st it's not a question, it's an answer to the linked question. 2nd, the "answer" you give (gateway) is basically what OP's ask if it's necessary. As usual, the answer is "depends". However, in my experience working with Spring, it's not 99% of the time. Even my answer (decoupling from Spring) is counterproductive, but I had to illustrate how to decouple "core" from "framework". 3rd, Questions like "what do you think" only leads to debate and opinionated answers.
    – Laiv
    Commented Nov 24, 2022 at 10:34
  • Thank you for your answer and I understand your arguments. I will do that
    – Adri
    Commented Nov 24, 2022 at 14:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.