0

Recently while going through spring's mongotemplate they touched upon repositories (here). Say, for example, a CRUD repository, is it a repository made for all your CRUD operations? Could anyone explain this in more simple terms, what exactly is the puropse of a repository?

Sandeep B
  • 765
  • 1
  • 6
  • 19
  • 1
    Reads like a tautology: "Is a CRUD repository a repository for all your CRUD operations?" Why yes, yes it is.... – duffymo Jul 24 '14 at 11:28
  • See also http://stackoverflow.com/questions/3037605/responsibilities-of-service-and-repository-layers – Raedwald Jul 24 '14 at 11:43

1 Answers1

2

Persisting data is all about CRUD (Create/Read/Update/Delete), but you can use different technologies to implement these operations.

The link you provided happens to choose MongoDB, a populate NoSQL document database. Spring Data can also work with relational SQL databases, object databases, graph databases, etc.

The beauty of designing to an interface, and the true power of Spring, is that you can separate what you need accomplished from the details of how it's done. Spring dependency injection makes it easy to swap in different implementations so you don't have to be bound too tightly to your choice.

Here's a simple generic DAO interface with CRUD operations:

package persistence;

public interface GenericDao<K, V> {
    List<V> find();
    V find(K id);
    K save(V value);
    void update(V value);
    void delete(V value);
}

You could have a HibernateGenericDao:

package persistence;

public class HibernateGenericDao implements GenericDao<K, V> {
    // implement methods using Hibernate here.
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • So, if I maintain a CRUD repository and wire to all my Dao's and use generic methods like save, delete, update, saveOrUpdate, etc.. and if later I decide to choose instead of a RDBMS solution a NoSql solution its just a matter of configuration. Do you see some more examples like this? Apart from CRUD's why would I need to create some custom repository? – Sandeep B Jul 24 '14 at 11:44
  • Sorry, I'm confused. DAOs are CRUD repositories. It sounds like you already have multiple DAOs that map 1:1 with classes. You may choose to write a generic repository that applies to all persistent classes, but you don't need both. – duffymo Jul 24 '14 at 11:56
  • Hibernate is one choice among many for implementing DAOs with CRUD operations. You may choose straight JDBC, MongoDB, or anything else in place of Hibernate, but the interface they implement would be the same regardless. – duffymo Jul 24 '14 at 12:05
  • Ok, so it is more or less like a genericDao which has all the basic utility methods (save, update, delete, etc..). I guess if anybody who used hibernate would surely have this generic dao in their code. Sorry for me being so ignorant about this, is it really the same thing, then what is the need of this marker interface is it more or less like a good pattern which one must incorporate in their code and spring has just formalized the way we used to go about this earlier? – Sandeep B Jul 24 '14 at 12:11
  • Wrong. Choosing to use Hibernate does not force you to use a generic interface. It's not a marker interface like Remotable or Serializable. You can write a Hibernate DAO or any other class without an interface at all. – duffymo Jul 24 '14 at 12:19
  • I mean repository interface of Spring which is a marker. – Sandeep B Jul 24 '14 at 12:23