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 question
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
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
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
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 ?