I'm working in an architecture, it is going to offer a REST API for web client and mobile apps. I'm using Spring(spring-mvc, spring-data-jpa, ...). The domain model is coded with JPA specification.
I'm trying to apply some concepts of clean architecture (https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html). Not all, because I'm going to keep the JPA domain model.
The actual flow through the layers is this:
Front end <--> API Service -> Service -> Repository -> DB
- Front end: web client, mobile apps
 - API Service: Rest Controllers, here I use converters and dto's and call services
 - Service: Interfaces with implementations and they contain business logic
 - Repository: Repository interfaces with automatically implementations(done by spring data jpa) which contatin CRUD operations and maybe some sql queries
 
My doubt: Should I use an extra layer between service and repository?
I'm planning this new flow:
Front end <--> API Service -> Service -> Persistence -> Repository -> DB
Why to use this persistence layer? As it says in the clean architecture article I would like to have a service implementation(business logic or use case) that access an agnostic persistence layer. And not changes will be needed if I decide to use another "data access" pattern, for example if I decide to stop using repository.
class ProductServiceImpl implements ProductService {
    ProductRepository productRepository;
    void save(Product product) {
        // do business logic
        productRepository.save(product)
    }
}
So I'm thinking use a persistence layer like this:
class ProductServiceImpl implements ProductService {
    ProductPersistence productPersistence;
    void save(Product product) {
        // do business logic
        productPersistence.save(product)
    }
}
and implementation of the persistence layer like this:
class ProductPersistenceImpl implements ProductPersistence {
    ProductRepository productRepository;
    void save(Product product) {
        productRepository.save(product)
    }
}
So I only need to change the implementations of persistence layer, left the service without changes.Coupled with the fact that the Repository is related with the framework.
What do you think? Thanks.




