Search code examples
javaspringspring-datasolid-principles

Spring data custom repositories and interface segregation principle


I'm working on my own model of repositories and I have this one:

enter image description here

I'm following the Interface Segregation principle. I have a ReadRepository to define only read operations, PaginationRepository to add Pagination over the ReadRepository methods and CrudRepository with all CRUD operations. Finally, I have BaseRepository with the behavior of PaginationRepository and CrudRepository. As you can see on the picture, the BaseRepository is extending from ReadRepository Twice through its two parents. This model gives me the flexibility to create the specific entity repositories (readOnly, readOnly + Pagination, crud operations or crud operations + pagination) but, is the transitive inheritance a bad practice?


Solution

  • Deep inheritance structures are only considered a problem when they are used to inherit implementations. With interfaces, the only potential problem I see is that you might end up with different methods from different interfaces that become one when both interfaces get implemented. But since all the interfaces are under your control, that shouldn't be a problem.