2

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?

2 Answers2

1

Generic DAO design pattern comes into picture when more then one DAO classes wants to communicate with database (as per example CRUD(Create,Read ,Update and Delete) ),with out this design pattern you will end up writing separate code to make database call (using session) for each of your DAO classes which is a tedious work in general because with each new implementation of DAO classes you have to write you own code to deal with database.

Below are some Pros and cons of using Generic DAO.

Note: Details give below are what I have learned from answers given to SO Question Pros and Cons of the use of DAO pattern

Pros

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

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

3. 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]

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

Cons

1. If you handwrite the DAOs, then the code can become tedious and repetitive you have to use code generators/templates and ORM.

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

A- After Observing above pros and cons I used Generic DAO in my application as abstraction layer to communicate with database in Terms Of CRUD which actual helped me reduces lots of duplicate code to do same thing other DAOs.At first it will take time to get used to it of afterwards use of Generic DAO will make you life easy.

Community
  • 1
  • 1
Dev
  • 2,326
  • 24
  • 45
  • Thanks @Dev. I am worried about unit testing, If I change the functionality of any generic function I need to do end-to-end test instead of testing one component. In that case how this generic pattern helps. – Neelakandan Sivathanu Nov 19 '15 at 06:00
  • @Neelkandan Sivathanu its other way If you change the functionality of any generic function you don't need to do end-to-end test instead do testing of only one component , as per example i have method `public T persist(T entity)` which takes an domain object and do `session.saveOrUpdate(entity)` ,suppose i have change some logic here just need to make sure if changes getting reflected with a domain object and will get reflected in all domain objects as because if use of java generic. – Dev Nov 19 '15 at 06:20
  • I am also in the same context. Thanks. – Neelakandan Sivathanu Nov 19 '15 at 10:58
1

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.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67