Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Auto persistency (http://www.velocityreviews.com/forums/t320571-auto-persistency.html)

Batista, Facundo 08-05-2003 11:24 AM

Auto persistency
 
Example of class. Just an object between all of them in my program:


class Example:
def __init__(self, value):
self.value = value
return

def getValue(self):
return self.value

def setValue(self, value):
self.value = value
return


I want to implement persistency. I think it's a good idea to each object be
the responsible of it's own persistence in a file, in a sql server, anywhere
(opinions?).

Adding persistence:

class Example:
def __init__(self, value):
self.value = value
# create in sql server
return

def getValue(self):
return self.value

def setValue(self, value):
self.value = value
# modify in sql server
return


Now I have two questions:

1) How do I implement the dead of the object. I need it to erase
itself from the sql server. Should I define the __del__ method?

2) When the program starts, how do I retrieve all the objects from
the sql server to memory? (considering that only the object knows how it's
persisted).

Thanks for all.


.. Facundo


Timo Warns 08-06-2003 07:07 AM

Re: Auto persistency
 
Batista, Facundo wrote:
>
> [Example of class with get- and set-methods]
>
> I want to implement persistency. I think it's a good idea to each object be
> the responsible of it's own persistence in a file, in a sql server, anywhere
> (opinions?).


This is some kind of a design pattern often called "data access
objects". Java has this already built-in with its JDOs.
Hava a look at this design pattern and how it is implemented in other
languages or python packages.
Do you need any kind of capabilities like transactions?

> [...]
>
> Now I have two questions:
>
> 1) How do I implement the dead of the object. I need it to erase
> itself from the sql server. Should I define the __del__ method?


You probably should create another method like "drop". If you implement
the __del__ method, you will not have any persistency, because any time
your object is deleted, the data will be deleted in your data source.

>
> 2) When the program starts, how do I retrieve all the objects from
> the sql server to memory? (considering that only the object knows how it's
> persisted).


You could implement a factory, that generates the objects and loads the
data by a given primary key.

HTH, Timo

Timo Warns 08-07-2003 10:25 AM

Re: Auto persistency
 
Timo Warns wrote:
> Batista, Facundo wrote:
> >
> > [Example of class with get- and set-methods]
> >
> > [...]

>
> This is some kind of a design pattern often called "data access
> objects". Java has this already built-in with its JDOs.


Actually "built-in" is a little misleading ;-) There is an optional
package from Sun available. Strictly it is not part of the language.
Sorry for that.

Timo


All times are GMT. The time now is 08:30 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.