Object Models - decoupling data access - good examples ?

shearichard at gmail.com shearichard at gmail.com
Sat Aug 4 20:04:38 EDT 2012


I'm interested in best practice approaches to : decoupling data access code from application code; and translations between database structures and domain objects.

For some time I've done database access by in a particular way and while I think it's OK it's not very pythonic so I'd be interested in hearing of other approaches - even better if there are open source examples whose code might be read.

I should say I'm talking relational database here and, for various reasons, ORMs are not involved.

So this is what I do (in a one class project call 'bar') in order to allow objects of type foo to be fetched/inserted/updated


bar/foo.py
bar/bardb/bardbConnection.py
bar/bardb/BusinessLogic/fooManager.py
bar/bardb/BusinessObject/foo.py
bar/bardb/DataAccess/fooDB.py

And this is what they actually do :


bar/foo.py
The class as the outside world knows it 

bar/bardb/bardbConnection.py
Manages database connection

bar/bardb/BusinessLogic/fooManager.py
Exposes methods used by bar/foo.py such as 'save'/'update' etc and implements necessary validation etc

bar/bardb/BusinessObject/foo.py
A collection of getters/setters which does any translation between types necessary on the way to/from the rdbms

bar/bardb/DataAccess/fooDB.py
Implements the actual SQL necessary for the relevant interactions with the database

As I say this works OK for me but I'd be interested to hear of what others do.

Thanks

Richard.




More information about the Python-list mailing list