springhibernategenericsgenericdao

What is the pros and cons of using Generic DAO and Generic Service Pattern in Spring MVC with Hibernate


I have a thought to implement Generic DAO and Generic Service in my new project. I have seen lot of examples in the web.

Before start I want to know the pros and cons of using this design pattern.

Can any one tell Is it advisable to use this pattern?


Solution

  • I think, it will be better to have an other opinion about DAO and generic DAO. Some words about Pros (My suggestions are valid if you use ORM, Hibernate for an example, not plain JDBC).

    1. Creates a nice abstraction layer of the actual storage system.

    It is a marketing bullshit. In real life we have problems to migrate between various RDBMS (Oracle RDBMS -> PostgreSQL). Not speaking about change of a storage system type (RDBMS -> NoSQL for example).

    1. Provide a more object-oriented view of the persistence layer.

    No! It is very hard to do properly. Most DAO implementations have a dozens of methods like

    getSomething(String x, String y, String z);
    getSomethingOther(String x, String z); 
    
    1. Provide a clean separation between the domain class and code which will perform the data access from databse.[using JDBC,ORM[like hibernate] or JPA].

    May be, but usefulness of this separation is exaggerated.

    1. Once you have the general CRUD flow set, the same layout can be repeated for other DAOs.

    It is correct.