Thank you for the adice.  I see that exclusive relaince on this approach 
would prohibit transactions.
 
The use case at hand is an object storing some information about itself.  
As only one object will be saved at a time, I'm thinking this appraoch is 
OK.  However, I will make the method private.
 
Please let me know if if you think I should do otherwise.
 
Thank you again,
 
~Victor

On Thursday, July 18, 2013 5:02:22 PM UTC-7, Mauricio de Abreu Antunes 
wrote:

> "meaning that a single commit() should address all the objects that 
> are related to a particular operation." 
>
> i commited it to my mind. :) 
>
> 2013/7/18 Michael Bayer <mik...@zzzcomputing.com <javascript:>>: 
> > 
> > On Jul 18, 2013, at 6:52 PM, Victor Reichert 
> > <vfr...@gmail.com<javascript:>> 
> wrote: 
> > 
> >> HI All, 
> >> 
> >> I'm working on my first SQL Alchemy project.  I'm thinking I'm going to 
> define a commit method for all the objects I want persist, I'm thinking 
> something like: 
> >> 
> >>     def commit(self): 
> >>         session = Session() #Session is a global sessionmaker 
> >>         session.add(self) 
> >>         session.commit() 
> >>         session.close() 
> >> 
> >> Is that a good practice? 
> > 
> > This is an antipattern.   You should keep the means of persistence 
> separate from the objects that you are persisting, and you should be 
> thinking in terms of use cases as far as how to structure transactions, 
> meaning that a single commit() should address all the objects that are 
> related to a particular operation. 
> > 
> > A bad metaphor might be, suppose we wish to write a collection of 
> sentences to a file.   The antipattern approach to me looks like this: 
> > 
> > class Sentence(object): 
> >     def __init__(self, text): 
> >         self.text = text 
> > 
> >     def write(self): 
> >         handle = open(self.file, "a") 
> >         handle.write(self.text) 
> >         handle.close() 
> > 
> > file = "myfile.txt" 
> > for sentence in sentences: 
> >     sentence.write() 
> > 
> > While thinking in terms of operations instead of objects looks like 
> this: 
> > 
> > class Sentence(object): 
> >     def __init__(self, text): 
> >         self.text = text 
> > 
> > handle = open(self.file, "w") 
> > for sentence in sentences: 
> >     handle.write(sentence.text) 
> > handle.close() 
> > 
> > besides the obvious efficiency, we don't force each sentence to deal 
> with the target file in isolation of all the other sentences.  Dealing with 
> the file's lifespan is outside of the scope of the thing we're putting in 
> the file. 
> > 
> > By "use case", this depends a lot on what kind of application this is.   
> If it's a web application, you want a transaction per request.  If it's a 
> short console script, you want a transaction for the life of the script 
> itself.  There's a lot more written about this here:   
> http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#session-frequently-asked-questions
>  
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "sqlalchemy" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to sqlalchemy+...@googlegroups.com <javascript:>. 
> > To post to this group, send email to 
> > sqlal...@googlegroups.com<javascript:>. 
>
> > Visit this group at http://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/groups/opt_out. 
> > 
> > 
>
>
>
> -- 
> Mauricio de Abreu Antunes 
> Mobile: (51)930-74-525 
> Skype: mauricio.abreua 
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to