I combed my design patterns and came across the thought that I could not find a good answer anywhere. Therefore, maybe someone with more experience can help me.
Is the DAO pattern designed to only access data in a database?
Most of the answers I found imply yes; in fact, most who speak or write according to the DAO pattern usually assume that you are working with some kind of database.
I do not agree. I could use DAO as follows:
public interface CountryData { public List<Country> getByCriteria(Criteria criteria); } public final class SQLCountryData implements CountryData { public List<Country> getByCriteria(Criteria criteria) {
Here I have a DAO interface and 2 implementations, one of which works with an SQL database, and one that works, say, in a graph data structure in memory. It's right? Or is it a graph implementation designed to be created in some other layer?
And if that's right, what is the best way to ignore the specific concrete details that each DAO implementation requires?
For example, take the class I criterion above. Suppose this is so:
public final class Criteria { private String countryName; public String getCountryName() { return this.countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } }
For SQLCountryData, you must somehow map the countryName property to the SQL identifier so that it can generate the correct SQL. For the GraphCountryData, some Predicate Object can be created against the countryName property to filter the vertices from the graph, which does not work.
What is the best way to abstract away such details without associating client code that works with abstract CountryData with specific concrete implementation data?
Any thoughts?
EDIT:
The example that I included in the criteria class is simple enough, but think about whether I want to allow the client to build complex criteria, where they should not only specify the property to filter, but also the equality operator, logical operators for composite criteria and value.