First of all, I'm sorry if this subject has already been treated but I didn't find what I was exactly looking for. I'm working on a ERP and we're trying to make some refactoring of the code. The main subject is that we currently don't use any DAO pattern, which could be a problem in the future if we need to access the "database" differently.
In brief, our architecture would aim to this pattern :
Bean or Webservices call what we call the "Transaction Layer" (encapsulates Services so that some stuff can be exposed via WS and do other things). That layer calls the Services, that will call other Services or DAOs.
1) Entity
public class MyObject{
private String arg1;
private List<SomeOtherObject> arg2List;
}
2) DAO
public interface MyObjectDAO {
void save();
List<MyObject> findAllObjects();
// Some other queries
// ...
}
3) MyObjectService
@Service
public class MyObjectService{
@Autowired
MyObjectDAO dao;
@Autowired
MyOtherObjectDAO otherDao;
public void createObject(String arg1Dto, List<MyOtherObjectDto> arg2Dto){
// How to deal with arg 2 ?
MyObject obj = new MyObject();
obj.setArg1(arg1);
obj.setArg2(myEntityRepresentingArg2);
dao.save(obj1);
}
}
3) Transaction Layer
public class{
// Many many things...
//Method called from the Beans
@Transactional(rollbackFor=Exception.class)
public void serviceCall(SomeDto arguments){
myObjectServices.createObject(arguments.getArg1(), arguments.getArg2());
}
}
My question is about best practices :
First of all, we use Hibernate and JPARepository to manage entities. So I'm guessing the call to repositories should be done in DAOImpls ? What about the queries we do to the database (that is to say JPAQuery with joins, selects, etc) and do projections ? That way the DAO would return DTOs...
We are also not sure on where to use the DTOs. Where the line should be between the use of DTO in the "Transaction Layer" and the entities one in DAOs ? Should the DTO be passed to the Service classes and then pass entirely the entities to the DAO layer ? Or should we pass only arguments to the DAO and it creates the entities itself (the problem is that it leads to some enormous method signatures).
Thank you very much and do not hesitate to ask questions if needed !