Saving changed member data

D

DeveloperDave

Hi,

I'm trying to think of the best way to track changes to my member
data, so I can write changes back to a database (currently Im using a
flat file, but eventually I'll be using a terrible API that forces me
to make one update at a time over HTTP) in the most effective way.

I have a class that has three or four member variables. Some members
are simple types such as std::string, int etc, others are custom
classes with there own members.

Objects are created from the class by reading data from the DB, and
passing the data to constructors.

After an object has been created the member data can be manipulated
using Set/Get operations.

I want to be able to pass my collection of SimpleObjects to another
class that reads/writes to the DB. Rather than writing the entire
object to the DB everytime, I want to just write changed members.

One approach I've taken is to create an enum called EProperties in my
class, with a value for each member variable. I have then created a
set that contains my EProperties type. Everytime a Set operation is
called on the object, the appropriate EProperties is inserted into the
set (the set becomes the history of the object).

Finally I method called GetHistory() returns the set, so that another
class can see a list of the members which have been modified. It can
then process them individually taking the appropriate action.

e.g.

class SimpleObject
{

public:
SimpleObject();

int GetID();
void SetID();

std::string GetName();
void SetName(std::string name);

std::string GetModel();
void SetModel(std::string model);

enum EProperties
{
id,
name,
model
};

std::set& GetHistory()

private:
int m_id;
string m_name;
string m_model;

set<EProperties> m_history;
};

In my class that updates the database, I call GetHistory, iterate
through the list, and pass each EProperty through a switch statement.
This works, but I don't like the fact that I'm effectively adding two
separate bits of data for each member. I considered creating a
template wrapper class, that would contain a EProperties along with
the required type.

Does anyone else have any suggestions of how I could achieve this?


Cheers
 
T

Thorsten

Hi,

I'm trying to think of the best way to track changes to my member
data, so I can write changes back to a database (currently Im using a
flat file, but eventually I'll be using a terrible API that forces me
to make one update at a time over HTTP) in the most effective way.

I have a class that has three or four member variables.  Some members
are simple types such as std::string, int etc, others are custom
classes with there own members.

Objects are created from the class by reading data from the DB, and
passing the data to constructors.

After an object has been  created the member data can be manipulated
using Set/Get operations.

I want to be able to pass my collection of SimpleObjects to another
class that reads/writes to the DB.  Rather than writing the entire
object to the DB everytime, I want to just write changed members.

One approach I've taken is to create an enum called EProperties in my
class, with a value for each member variable. I have then created a
set that contains my EProperties type.  Everytime a Set operation is
called on the object, the appropriate EProperties is inserted into the
set (the set becomes the history of the object).

Finally I method called GetHistory() returns the set, so that another
class can see a list of the members which have been modified.  It can
then process them individually taking the appropriate action.

e.g.

class SimpleObject
{

public:
    SimpleObject();

    int GetID();
    void SetID();

    std::string GetName();
    void SetName(std::string name);

    std::string GetModel();
    void SetModel(std::string model);

    enum EProperties
    {
       id,
       name,
       model
    };

    std::set& GetHistory()

private:
    int m_id;
    string m_name;
    string m_model;

    set<EProperties> m_history;

};

In my class that updates the database, I call GetHistory, iterate
through the list, and pass each EProperty through a switch statement.
This works, but I don't like the fact that I'm effectively adding two
separate bits of data for each member.  I considered creating a
template wrapper class, that would contain a EProperties along with
the required type.

Does anyone else have any suggestions of how I could achieve this?

Cheers

I don't know if you gain that much by seperating single items to be
saved, as long as your class is that simple. writing the whole thing
in one go seems ok.

If it's really necessary, I would write a streaming method for each
member and put a class method pointer to that method into the set.
This would keep the saving class from having to know too much about
the internals of this class.
 
F

Francesco S. Carta

Hi,

I'm trying to think of the best way to track changes to my member
data, so I can write changes back to a database (currently Im using a
flat file, but eventually I'll be using a terrible API that forces me
to make one update at a time over HTTP) in the most effective way.

Have a look to SQLite, shall you decide to switch to the DB-way.

Just half a cent ;-)
 
T

tni

I want to be able to pass my collection of SimpleObjects to another
class that reads/writes to the DB. Rather than writing the entire
object to the DB everytime, I want to just write changed members.

One approach I've taken is to create an enum called EProperties in my
class, with a value for each member variable. I have then created a
set that contains my EProperties type. Everytime a Set operation is
called on the object, the appropriate EProperties is inserted into the
set (the set becomes the history of the object).

Finally I method called GetHistory() returns the set, so that another
class can see a list of the members which have been modified. It can
then process them individually taking the appropriate action.

A different approach would be to make your data objects immutable and
create a new version whenever you change something. You can keep the
last version that was written to the DB around to able to make diffs.

If you have complex data (that is expensive to copy), take a look at COW
(copy on write); e.g. Qt makes extensive use of that.

If the data you are updating is in a single table, you won't really gain
anything by doing selective updates anyway (unless your network
connection to the DB is very slow).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top