liveBook · Manning

Interface Segregation Principle

As explained in Spring Start Here, Second Edition

Interface segregation is a design principle that favors several focused interfaces over one interface containing operations that clients do not need.

A single interface with operations a client never uses can make an application more complex. Interface segregation favors focused contracts, so each client can choose the operations it needs. Spring Data applies this idea through repository interfaces. section 14.2

Choose a Spring Data contract to match the repository’s needs:

  • Use CrudRepository when CRUD operations are enough; it avoids adding paging and sorting operations. section 14.2
  • Use PagingAndSortingRepository when the application also needs paging and sorting. section 14.2
  • Choose a technology-specific contract, such as JpaRepository or MongoRepository, for operations tied to that persistence technology. section 14.2
Figure 14.7 To create a Spring Data repository, you define an interface that extends one of the Spring Data contracts. For example, if your app only needs CRUD operations, the interface you define as a repository should extend the CrudRepository interface. The app adds a bean that implements the contract you define to the Spring context, so any other app components that need to use it can simply inject it from the context.

See how a repository interface can extend CrudRepository when only CRUD operations are needed. section 14.2

How Spring Start Here, Second Edition covers it

How deep it goes
Defines the principle and illustrates it with CRUD, paging, sorting, and technology-specific repository contracts.
Examples
Contrasts CrudRepository with PagingAndSortingRepository, JpaRepository, and MongoRepository.
What you can do after
Apply it via Spring Data by choosing a repository contract that matches the operations an application needs.
How it connects
Connects repository interfaces with CRUD, paging and sorting, JPA, and MongoDB operations.

Also explained in

Compare how 4 books explain Interface Segregation Principle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.