Search code examples
javaweb-servicesdaodto

Merge data from two DAO into one TO


I have a siutation where I want to work with one business object (TO). However the data that this TO consist of comes from 2 different data sources:

  • The application's database, which will be accessed through JPA and one Entity
  • An old system's data, which will be accessed through a Webservice

In later phases of this project, all the data will be moved into the application's database. Therefore I want to have one business object (TO) to represent all this data.

My approach is one of the following:

1) Have a DAO for the Entity and another DAO for the Old System. Then yet another DAO on top of these, which will create the Business Object that I want to use in the rest of the application.

2) Have only one DAO that will retrieve some data from the Entity and some from the Old System.

What's your thoughts about this?


Solution

  • You need to introduce a service layer on top of Dao's (Entity, Old system) and inject (assuming you're using Spring) all the Dao's into the service. Option 1 is better however just create two Dao's inside a Service and create the Business Transfer Object that will be used throughout the application. The workflow would be UI -> PersonService -> Dao1 (app db), Dao2(Webservice). The PersonService will have the necessary business logic if any. Additionally, you can create a PersonDaoFactory inside the PersonService to abstract the creation of Dao's inside Service layer. So, at the end we will have something like below from the Controller or UI Layer(an hypothetical scenario as i do not know the domain model):

    Person person = personService.findUserById(personId);
    Product product = person.getProducts();
    Comment comment = person.getComments();
    Friends friends = person.getFriends();
    

    Additionally, Think of a problem that many DAO solve and one big DAO would not solve.