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
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