Saving changed member data

Discussion in 'C++' started by DeveloperDave, Aug 31, 2010.

  1. 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
     
    DeveloperDave, Aug 31, 2010
    #1
    1. Advertising

  2. DeveloperDave

    Thorsten Guest

    On Aug 31, 6:12 pm, DeveloperDave <> wrote:
    > 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.
     
    Thorsten, Aug 31, 2010
    #2
    1. Advertising

  3. DeveloperDave <>, on 31/08/2010 09:12:58, wrote:

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

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Aug 31, 2010
    #3
  4. DeveloperDave

    tni Guest

    On 2010-08-31 18:12, DeveloperDave wrote:

    > 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).
     
    tni, Aug 31, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Kandell
    Replies:
    4
    Views:
    4,168
    eeebop
    Dec 10, 2004
  2. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,516
    Luis Esteban Valencia
    Jan 6, 2005
  3. Replies:
    1
    Views:
    692
    Rosanne
    Oct 11, 2005
  4. Jason
    Replies:
    2
    Views:
    514
    Jonathan Mcdougall
    May 13, 2006
  5. mxbrunet
    Replies:
    1
    Views:
    219
Loading...

Share This Page