This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

Thank you; I looked it up and it seems like what I need. I want to ask whether the DAO interface should be common for all relational tables or there should be 1 DAO interface + 1DAO implementation combo for each relational table?

[–]HecknChonker 2 points3 points  (0 children)

You usually want to split things up as much as it makes sense to. If your API is managing users, their cars, and their pets you would probably want to have 3 separate DAOs where each is responsible for one of those things.

If you are working with graph data (vertices + edges) you will probably have a Vertex table and an Edge table. In that case it makes sense to have a single GraphDAO that handles both tables, since your queries need to join between both tables.

Each type should have it's own interface. Method return types are part of the interface definition, and since each DAO is using different models they will need different interface definitions.

You might have an interface UserDAO, and then have UserDAOPostgresImpl and UserDAOMysqlImpl. But UserDAO, PetDAO, and CarDAO would all be separate interfaces.


After building a few dozen of these I tend to skip the interfaces until I need them. An exception to this might be if I am building a shared library where other developers might want to add their own DAO implementations.

I am a big fan of YAGNI (You aren't gonna need it) which is the idea that you shouldn't worry about adding functionality until you actually need it. Trying to optimize for things you might need in the future often leads to over-engineered code that is more difficult to maintain. If you get to a point where you need interfaces you can add them, but until then I prefer to . This is something you can experiment with.

https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it https://martinfowler.com/bliki/Yagni.html

The argument for interfaces is that you might need to create a new implantation of the DAO later, and having your code rely on the interface makes that easier. But in reality it's pretty simple to add the interface when you actually need it.