1

I am working on refactoring the data access layer for a project where I need to provide the business logic layer developers with a single interface for all DB interactions. Currently there is just one DAOImpl class implementing this interface but the class has bloated to 15000+ lines of code. Now I wish to move the methods from this class into multiple classes based on the type of object they handle. The approach I have thought of is -

  1. Keep the DAOInterface with all methods as is
  2. Implement a DAOImpl class which implements the DAOInterface but will not have any logic in any of the methods
  3. Implement Object specific DAOImpl classes which extend the DAOImpl and implement the DAOInterface and provide the actual DAO implementation for all object specific methods.
  4. Change the current DAOFactory class to provide instances of object specific DAOImpl based on some identifier passed from the business logic layer.

I just wanted to validate my approach in this forum to see if I am doing things the right way or is there a better solution/pattern for this problem.

3
  • Hello, Do you not have any common code, which may used for all type of instances of Class DAOImpl classes extended with DAOImpl and Implemented by DAOInterface. If not then your approach is Okay. Else you have to improve your approach. Commented Apr 25, 2013 at 5:26
  • If the objective is to reduce the lines of code in single DAO. Move the implementation code to Specific DAO's(Sub). Example : public Interface DAOInterface { public void saveObject() ; } public Class DAOImpl implements DAOInterface { public void saveObject() { subDAO.saveObject() //subDAO injected as DI} and public Class FirstSubDAO { public void saveObject() { // Implementation logic}} Commented Apr 25, 2013 at 5:52
  • You could make an interface that extends all the other interfaces and the make a java.lang.Proxy for that interface. When creating the proxy you can retrieve all methods that the interface has, finally mapping the interface method to a concrete call to the implementation of that method. Commented Apr 25, 2013 at 15:01

1 Answer 1

1

I would suggest to have it in Facade like style. The main DAOImpl has references to all the sub DAOs delegating the calls to appropriate one.

UPD: to illustrate the approach

interface DAO {
 void doSomethingUser();
 void doSomethingProject();
}

class DAOImpl {
  private UserDAOImpl;
  private ProjectDAOImpl;
  public void doSomethingUser() {
    UserDAOImpl.doSomethingUser();
  }
  public void doSomethingProject() {
    ProjectDAOImpl.doSomethingProject();
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Tej Kiran - I havent encountered any common code yet but my plan is if I come across anything that is not fit to go into object specific DAOImpl then it can reside at the Generic DAOImpl (top level implementation class which extends DAOInterface). If I dont come across anything like that, I will mark the DAOImpl an abstract class and the Factory will never return an instance of the DAOImpl class.
@Kumar - The objective is to refactor the code, optimize the implementation and also reduce the lines of code in a single DAO as the current DAO implementation is proving to be our performce bottleneck.
@user2185805 My example code will be usefully in reducing the code in single DAO.
@StanislavL Thank you , Yes this will reduce the lines of code in single DAO. My example code in comments is similar to your example.Where I suggested to use Spring DI to inject Specific DAO in DAOImpl.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.