[Dev-sig] Pattern/technique/class-wrapper question

Bryan Backer bbacker at yahoo.com
Thu Nov 2 23:13:40 PST 2006


Tom,
The DAO pattern seems like a nice, abstract description
if you're going to implement the actual
serialize/unserialze or freeze/thaw logic
 (like your included examples).

I don't know what your target language is, but
if you're going to be implementing many objects or
structs like this, you might consider using something like hibernate
(hibernate.org) or some other serializing package
so you don't have to roll your own peristence code.
http://www.hibernate.org/328.html in particular
seems interesting. (warning - I have not used hibernate
myself, but know folk on my team that have spent
*many* days finding errors in hand rolled persistence
code...

If you're using a language like perl, you could
consider some of the modules from cpan.org that
that do object or struct (likely a hash in perl)
persistence to a DB via DBI calls. I'd suggest
reading chapter 14 of this book:
http://www.manning.com/conway/
which covers serializing to binary formats, flat
files, and relational DBs.




--- Robert Leyva <mrflash818 at geophile.net> wrote:

> My thought for you would be:
> 
> DAO - http://java.sun.com/blueprints/patterns/DAO.html
> DAO -
> http://www.corej2eepatterns.com/Patterns2ndEd/DataAccessObject.htm
> 
> Yes, they are examples using Java, but the point in patterns is the
> _idea_
> of how to design it regardless of the specifics.
> 
> 
> 
> > 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
> >
> 
> 
> -- 
> "Knowledge is Power" -- Francis Bacon
> 
> Robert Leyva
> mrflash818 at geophile.net
> 
> 


-- bbacker at mail.yahoo.com


More information about the Dev-sig mailing list