data access object pattern implementation


Question

I would like to implement a data access object pattern in C++, but preferably without using multiple inheritance and/or boost (which my client does not like).

Do you have any suggestions?

1
3
9/22/2008 6:33:03 PM

Accepted Answer

OTL (otl.sourceforge.net) is an excellent C++ database library. It's a single include file so doesn't have all the complexity associated (rightly or wrongly!) with Boost.

In terms of the DAO itself, you have many options. The simplest that hides the database implementation is just to use C++ style interfaces and implement the data access layer in a particular implementation.

class MyDAO {
  // Pure virtual functions to access the data itself
}

class MyDAOImpl : public MyDAO {
  // Implementations to get the data from the database
}
5
9/22/2008 6:46:36 PM

A quick google search on data access object design patterns will return at least 10 results on the first page that will be useful. The most common of these is the abstract interface design as already shown by Jeff Foster. The only thing you may wish to add to this is a data access object factory to create your objects.

Most of the examples I could find with decent code are in Java, it's a common design pattern in Java, but they're still very relevant to C++ and you could use them quite easily.

This is a good link, it describes the abstract factory very well.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon