Hello people and happy new year
There i'm having a brainstorm on a design pattern. Let's talk in C++ and make abstraction of Qt for the moment.
[EDIT]: Just to make the post more clear: what i am trying to achieve, is a database object which has enough functionnality to provide the developper a good help after subclassing it. i want this base object to provide search method as well. i am not trying to do a huge thing which is automatically aware of fields and linked data and data types etc, but something usefull enough to speed up the dev of a larger database based app.
Basically here is what i have for now:
1) A base abstract (i'll explain why later) object named DBHelper:
It contains a string variable named "_table", and which has to be initialised in the constructor. So basically after this object is constructed, it "knows" on what database table to interact with.
The base object contains a private variable named _uid, which is of course a mandatory database field for any object, and is an auto increment in database. That's how i know if an object is abstract, up to date, or to be deleted etc...
This object also has 3 protected methods, each one to insert, update, or delete in a database (createObject, updateObject and deleteObject).
DBHelper also defines 2 pure virtual methods named "fieldsList" and "makeBinding". The first method has to return a QStringList containing the fields used by the subclassed object. The second method accepts a QSqlQuery reference as parametter, and allows the objects subclassing DBHelper to bind values to a query once it's prepared...
2) Several objects, for instance a DBEmployee which subclasses DBHelper and initialize it with the table named "employees". Let's assume DBEmployee contains one private variable "_name". It also implements the method "fieldsList" (Which in this case returns a 1-element QStringList containing "name", since the field in database is named like that). And of course, DBEmployee also implements makeBinding, so when the base DBHelper wants to create or update the object in DB, the binding are made...
Now let's suppose i want to add search functions to my DBEmployee object. i will probably decide to make something like this:
static QList<DBEmployee*> DBEmployee::findByName(const QString& name);
This method would call some magic in the base DBHelper (Which is of course not "subclassed" since we are talking about a static method).
But in this case, DBHelper can't get the table name, nor the bindings or fields list
i'm totally aware that there is no solution for this since i'm trying to achieve something (Calling something from a "base class" from a static method) which is programatically impossible... What i'm asking here, is the best solution (To make my DBHelper object as helpfull as possible, and of course avoid having SQL or database related code in the subclassing objects).
i'm not sure i'm clear enough. If you need more precisions just ask
Pierre.
Bookmarks