Search code examples
javanaming-conventionsdaoorganization

DAO naming conventions


I'm developing an app that needs to establish a connection with a DB. To operate through this connection I'm using DAO pattern. My question is related with the organization of the code and the name of interfaces and implementations. The current package structure is the next one:

  • mainpackage
  • mainpackage.model -> Models of each table to create objects with data fetched from DB
  • mainpackage.persistence -> ConnectionManager
  • mainpackage.persistence.dao -> Interfaces and implementations

About the naming of interfaces I thought that would be a good idea to use something like ClassDAO and DefaultClassDAO for the implementation as long I don't know how to name it. What do you think? Is there any convention for this?


Solution

  • I would use the convention described in "Domain driven design".

    • mainpackage
    • mainpackage.model -> contains 'repositories' as interfaces, like UserRepository, ProfileRepository describing the operations to manage your entities in the storage (store, find, etc). This way you don't leak that your storage is a DB.
    • mainpackage.model.db (or sql or whatever) -> contains SqlUserRepository, SqlProfileRepository implementing the interfaces in mainpackage.model and hitting a DB.

    You could even put the DB classes in a different sub-project. I think it's important to not put the DB at the center of your design and just considered it as an implementation detail.