C
cryptic_blade
I am working on the design for a new application and want to make the
program fully object oriented. I have built and used C++ classes
before, but never with a database driven application. Should classes
handle reading and writing their property data to the database table
themselves, or should they rely on an outside "DataHandler" custom
class or function specific to the application.
If I hard code specific database/table/query information into a
class's structure, it no longer become portable because it is
"bound" to database. I want to be able to reuse my classes and I
fear that writing storage routines "inside" the class definition
will render my classes less portable. How do most of you implement
class usage when class data need to be stored in databases and later
re-populated into a new instance of the class?
Example:
I want to build a ficticious object oriented program that allows the
user to enter in the year, make, and model of their favorite sports
car. Then that data is saved to a database table. The user also has the
ability to search the "cars" database table and display a list of
all previously saved cars. I also need to be able to reuse the classes
in other programs without needed to reconstruct the database and tables
used in the first program. It is possible that I may need to build
another program that deals with cars, but doesn't store the car
information in a database.
I already know I need a "Car" class, but should the Car class take
care of reading and writing to the database directly through methods I
expose? If I build-in database storage into the class I can't reuse
the class in another application without creating a database with a
"car" table. What to do???
Class: Car
Property1: Year
Property2: Make
Property3: Model
Method1: getCar(carID) <- populate object with database record data?
Method2: setCar(Year, Make, Model) <- update database record with
object data?
Hope I made my question clear enough.
Thanks in advance
program fully object oriented. I have built and used C++ classes
before, but never with a database driven application. Should classes
handle reading and writing their property data to the database table
themselves, or should they rely on an outside "DataHandler" custom
class or function specific to the application.
If I hard code specific database/table/query information into a
class's structure, it no longer become portable because it is
"bound" to database. I want to be able to reuse my classes and I
fear that writing storage routines "inside" the class definition
will render my classes less portable. How do most of you implement
class usage when class data need to be stored in databases and later
re-populated into a new instance of the class?
Example:
I want to build a ficticious object oriented program that allows the
user to enter in the year, make, and model of their favorite sports
car. Then that data is saved to a database table. The user also has the
ability to search the "cars" database table and display a list of
all previously saved cars. I also need to be able to reuse the classes
in other programs without needed to reconstruct the database and tables
used in the first program. It is possible that I may need to build
another program that deals with cars, but doesn't store the car
information in a database.
I already know I need a "Car" class, but should the Car class take
care of reading and writing to the database directly through methods I
expose? If I build-in database storage into the class I can't reuse
the class in another application without creating a database with a
"car" table. What to do???
Class: Car
Property1: Year
Property2: Make
Property3: Model
Method1: getCar(carID) <- populate object with database record data?
Method2: setCar(Year, Make, Model) <- update database record with
object data?
Hope I made my question clear enough.
Thanks in advance