0

For example, I have many tables, such as people car house and so forth, I want to know a good practice for the general DAO design.

In this case, people have cars and houses. car and house have it own id, maybe an auto_increment int value. and they also have a foreign key userId which is the PK of the user.

First step, I design a GenericDao to provide basic CRUD function for all tables.

And then?

I need to find all cars for user A, so I need a CarDao with function findAllCarsForUser(int userid), which may also have findCarsByName(String name) and findCarsByNameForUser(String name, int userid) and so on. For House, a same HouseDao is needed. If the people have other type of things, each should have an ObjectXXXDao.

But for the upper layer, should the XXXDao exposed to them?

I think it should not, so every DAO have a wrapped Service class, such as CarService HouseService and so on. But for functions in these service class, maybe only statement return XXXDao.findCarsByNameForUser(). nothing or little additional function is added to the service classes.

Or what function should be added to this XXXservice layer?

And If there are requirements to join the Car and House table, maybe a CarHouseDao and CarHouseService is needed.

I want to know, Is this a good design for a common DAO? If not, is there any good advice or examples?

NingLee
  • 1,477
  • 2
  • 17
  • 26
  • maybe read about [aggregate roots](http://martinfowler.com/bliki/DDD_Aggregate.html) in DDD. – Nathan Hughes Aug 18 '15 at 12:43
  • 1
    If your service do nothing other than calling DAO methods, they're pretty much redundant. The idea of a service layer is to implement the functional use cases of the application. Like for example "user buys a car", which consists in deleting the user's old car, change the owner of the bought car, decrease the user's amount of money, etc. A single service method will thus use several DAOs. – JB Nizet Aug 18 '15 at 12:55
  • @JBNizet Thanks for your answer, Is there any good examples for me? Is it good for every object to have a `ObjectDao` and a `ObjectService`? I also understand your example, but in actually, there must be some basic service, which only call the dao and do nothing. In this case, should I call the `ObjectDao` directly and discard the `ObjectService` class? – NingLee Aug 18 '15 at 13:03
  • Check these to understand http://stackoverflow.com/questions/32049609/call-service-vs-dao-from-another-service/32049986#32049986 http://stackoverflow.com/questions/3882108/dao-and-service-layers-jpa-hibernate-spring?rq=1 – Anudeep Gade Aug 18 '15 at 13:41

0 Answers0