OO Programming with Databases

Question:
I am getting into the object oriented features of VB4 and I am wondering where the back-end database (such as Access) comes in for small single user applications. You can seemingly just create lots of objects with lots of properties and not bother with the relational database unless you need a multi-user product. Agreed there are not many calls for single user systems today but in a small country such as Malta (my home), many businesses are single users.

Answer:
You’ve asked one of the important questions of object-oriented programming– how do I put the objects into the database? There are a couple of waysto do this.

First of all, you may want to “teach” each object how to load itself fromthe database. I use this method in a few places in the current applicationI’m using. Each individual object knows how to fill itself from thedatabase, once it has been passed an object reference to the currentdatabase. Each object also knows how to save itself back to the database.In my case, I cause the object to save itself any time a property changesin the object. I also have an override that tells the object not to saveitself immediately. This lets me do things like creating an empty object,filling it, and then telling it to save.

Another way to load/save objects is to have the collection object know howto save all the objects in one fell swoop. This takes a bit more power andwill result in some unnecessary saving; however, individual actions aremuch faster since no database action is required when changes are made.All the changes are committed at the end.

For more information on object-oriented programming, I would highlyrecommend Deborah Kurata’s Doing Objects in Microsoft Visual Basic 5.0.(There is also a version for Visual Basic 4.0) She goes through all theseissues and many more and provides techniques for getting around thesefundamental problems.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: