[Dev-sig] Pattern/technique/class-wrapper question
Emerson, Tom
Tom.Emerson at wbconsultant.com
Thu Nov 2 17:18:19 PST 2006
Someone asked a question about databases and classes, and a response
came up about "patterns". As I was reviewing and modifying some
spaghetti around here, I realized there are a couple of interesting ways
to go about this, and it made me wonder about my own previous answer
(wrap the DB calls to make the underyling storage mechanism transparent
to your program)
I also suspect this holds true of any language that lets you define
clsses or structures, regardless of whether or not that language is
considered to be "object oriented".
So, what would you recommend in this situation:
** you've got a block of data that you want to "persist" -- it could be
anything, common examples would be customer information (name, address,
etc.) or "order" information (header & details)
--> to ease the use of this block of data, you've created a "class" for
it (structure)
** you're told the underlying database "may change in the near future"
Should you:
--> have the class cough up an SQL insert string
[myObject class function] SQL_Insert
return "insert into blah.blah values (" + id + ","
+ name + "," + address + ")"
(where "id", "name", and "address" are variables local to the class
itself)
[mainline]
db.execute(myInstance.SQL_Insert)
--> incorporate the database calls within the class
Example: (same as above, but actually calls the DB routine as well)
[myObject class method] "save1"
sqlStr = "insert into blah.blah values (" + id + ","
+ name + "," + address + ")"
db.execute(sqlStr)
[mainline code]
Myinstance.save1
--> wrap the database calls in it's own class and pass an "instance"
of the object you want to persist as a parameter
[wrapped_db class method] wrapped_put_MyObject(SomeObject)
sqlstr = "insert into blah.blah values ("
+ SomeObject.id + "," + SomeObject.name + ","
+ SomeOjbect.address + ")"
db.execute(sqlstr)
[mainline]
wrapped_put_MyObject(myInstance)
--> wrap the database calls in a lowest-common-denominator "class" and
then write custom code to access the class members in order to feed the
wrapped-DB calls
[wrapped_db method] wrapped_prepareInsert(dataset)
_Dataset = dataset
_Idx = 1
_Values().clear <<internal array>>
[wrapped_db method] wrapped_addField(value)
_Values(_Idx) = value
increment(_Idx)
[wrapped_db method] wrapped_executeInsert
query = "insert into " + _Dataset + " values ("
for i = 1, _Idx - 1
query = query + _Values(i) + ","
loop [next]
[replace the right-most "," with a ")"]
db.execute(query)
[mainline code]
wrapped_prepareInsert("blah.blah")
wrapped_addField(myinstance.id)
wrapped_addField(myinstance.name)
wrapped_addField(myinstance.address)
wrapped_executeInsert
--> call those functions directly within the object
[myObject class method] save2
wrapped_prepareInsert("blah.blah")
wrapped_addField(id)
wrapped_addField(name)
wrapped_addField(address)
wrapped_executeInsert
[mainline]
myinstance.save2
--> something else entirely?
Tom Emerson
818-977-8828
More information about the Dev-sig
mailing list