Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about Spring-Data DDD repository pattern

Tags:

I don't know so much about DDD repository pattern but the implementation in Spring is confusion me.

public interface PersonRepository extends JpaRepository<Person, Long> { … }

As the interface extends JpaRepository (or MongoDBRepository...), if you change from one db to another, you have to change the interface as well.

For me an interface is there to provide some abstraction, but here it's not so much abstract...

Do you know why Spring-Data works like that?

like image 327
Sebastien Lorber Avatar asked May 12 '11 08:05

Sebastien Lorber


People also ask

What is the purpose of repository pattern in Spring data?

The repository pattern is extremely popular. In its modern interpretation, it abstracts the data store and enables your business logic to define read and write operations on a logical level. It does that by providing a set of methods to read, persist, update and remove an entity from the underlying data store.

Which is better CrudRepository or JpaRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

Should I use DAO or repository?

DAO would be considered closer to the database, often table-centric. Repository would be considered closer to the Domain, dealing only in Aggregate Roots. Repository could be implemented using DAO 's, but you wouldn't do the opposite. Also, a Repository is generally a narrower interface.

Is CRUD repository part of Spring data JPA?

Here in the following image Repository, CrudRepository and PagingAndSortingRepository belong to Spring Data Commons whereas JpaRepository belongs to Spring Data JPA. It is a base interface and extends Repository Interface. It extends PagingAndSortingRepository that extends CrudRepository.


1 Answers

You are right, an Interface is an abstraction about something that works equals for all implementing classes, from an outside point of view.

And that is exactly what happens here:

  • JpaRepository is a common view of all your JPA Repositories (for all the different Entities), while MongoDBRepository is the same for all MongoDB Entities.
  • But JpaRepository and MongoDBRepository have nothing in common, except the stuff that is defined in there common super Interfaces:

    • org.springframework.data.repository.PagingAndSortingRepository
    • org.springframework.data.repository.Repository

So for me it looks normal.

If you use the classes that implement your Repository then use PagingAndSortingRepository or Repository if you want to be able to switch from an JPA implementation to an Document based implementation (sorry but I can not imagine such a use case - anyway). And of course your Repository implementation should implement the correct interface (JpaRepository, MongoDBRepository) depending on what it is.

like image 115
Ralph Avatar answered Sep 19 '22 19:09

Ralph