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?