title: Better design for abstraction of Db with inheritance
body: <p>My application processes different kind of messages and stores them in a database. Right now I have used the following design:</p>
  
  <p>Database class:</p>
  
  <pre><code>class DbObject
  {
  public:
      // read/write object members from/to DB 
      virtual void readFromDb() = 0;
      virtual void writeToDb() = 0;
  
      // Other stuff, db connection etc.
      void doDbStuff();
  }
  </code></pre>
  
  <p>Base class for Messages:</p>
  
  <pre><code>class BaseMsg
  {
  public:
      // read/write object members from/to DB 
      virtual std::string toXml() = 0;
      virtual void fromXml(const &amp; std::string s) = 0;
  }
  </code></pre>
  
  <p>Message types A, B etc.</p>
  
  <pre><code>class MsgA : public BaseMsg, DbObject
  {
  public:
      std::string toXml();
      void fromXml(const &amp; std::string s);
  
      void readFromDb();
      void writeToDb();
  }
  </code></pre>
  
  <p>This design works well, each processed message gets it's own object, can be written to XML, read from XML, stored and read to/from the database with message specific stuff (implemented in MsgA).</p>
  
  <p>However, at the moment we are considering porting the application to a new platform, where the current database is not available, so we would use an additional database type. </p>
  
  <p>Normally I would have now have a base class for database access and child classes for each database type. But with the current design is not possible, because I don't want to have a MsgADatabase1 class and a MsgADatabase2 class, etc.</p>
  
  <p>Is there any design  pattern, where I could keep my current design in principle, but hide the currently used database type behind some kind of abstraction layer?</p>