Suppose I am writing a DAO to handle record retrieval from a database that is acting like a cache. It has a method getAllRecords().
CacheDAO::getAllRecords()
Now I also periodically need to refresh the cache subject to network availability i.e. if there is network connection available, I will try to fetch the latest data from a web source like XML, or JSON. If there is no network, I will simply grab whatever is already in the database cache. Let's call the method downloadRecords().
Option 1: Now, should I implement this downloadRecords() as a method inside the CacheDAO? CacheDAO will have 1 method accessing the database and 1 method fetching data from HTTP web source.
Option 2: Another option is to create a new DAO on its own: WebDAO, which somehow returns data from a HTTP source that are then inserted into CacheDAO database, followed by a call from CacheDAO.getAllRecords().
From my understanding is that a DAO implementation is tightly coupled to the storage system (e.g. database or web only) but not a mix of methods fetching data from different sources (database vs web service). This is to make it modular and flexible to swap in and out the DAO implementation class in case the underlying storage system (database, HTTP service) changes.
Comment?
CacheDAO::getAllRecords()
Now I also periodically need to refresh the cache subject to network availability i.e. if there is network connection available, I will try to fetch the latest data from a web source like XML, or JSON. If there is no network, I will simply grab whatever is already in the database cache. Let's call the method downloadRecords().
Option 1: Now, should I implement this downloadRecords() as a method inside the CacheDAO? CacheDAO will have 1 method accessing the database and 1 method fetching data from HTTP web source.
- CacheDAO::getAllRecords()
- CacheDAO::addRecord(...)
- CacheDAO::downloadRecords()
Option 2: Another option is to create a new DAO on its own: WebDAO, which somehow returns data from a HTTP source that are then inserted into CacheDAO database, followed by a call from CacheDAO.getAllRecords().
- CacheDAO::getAllRecords() // Read from database
- CacheDAO::addRecord(...) // Write to database
- WebDAO::downloadRecords() // Read from HTTP source
From my understanding is that a DAO implementation is tightly coupled to the storage system (e.g. database or web only) but not a mix of methods fetching data from different sources (database vs web service). This is to make it modular and flexible to swap in and out the DAO implementation class in case the underlying storage system (database, HTTP service) changes.
Comment?
Last edited: