[openstack-dev] [tricircle] DAL implementation

Vega Cai luckyvega.g at gmail.com
Thu Jul 30 07:02:33 UTC 2015


Hi folks,

In my current implementation, there are a core module and a models module.
Core module handles all the database stuff, start a session, issue sql
operation, then end a session. Models module invokes methods in core module
to access database, as showed below:

model.py
def get_site(site_id):
    core.get_resource(Site, site_id)

core.py
def get_resource(model, pk_value):
    # code to access database

To add context, I am going to implement like this:

model.py
def get_site(context, site_id):
    policy_check(context)
    core.get_resource(Site, site_id)

core.py
def get_resource(model, pk_value):
    # code to access database

So there is no need to embed session into context.

One advantage of embedding session into context is that you can combine
more than one method calls in one session, like:

model.py
def complex_operation(context):
    policy_check(context)
    with context.session.begin():
        core.operation1(context)
        core.operation2(context)

But this approach moves session handling from core module to models module
and core module just provides some utility methods.

I'm not sure which one is better.

BR
Zhiyuan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstack.org/pipermail/openstack-dev/attachments/20150730/a7b9016a/attachment-0001.html>


More information about the OpenStack-dev mailing list