<div dir="ltr"><div>Hi folks,</div><div><br></div><div>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:</div><div><br></div><div>model.py</div><div>def get_site(site_id):</div><div>    core.get_resource(Site, site_id)</div><div><br></div><div>core.py</div><div>def get_resource(model, pk_value):</div><div>    # code to access database</div><div><br></div><div>To add context, I am going to implement like this:</div><div><br></div><div>model.py</div><div>def get_site(context, site_id):</div><div>    policy_check(context)</div><div>    core.get_resource(Site, site_id)</div><div><br></div><div>core.py</div><div>def get_resource(model, pk_value):</div><div>    # code to access database</div><div><br></div><div>So there is no need to embed session into context.</div><div><br></div><div>One advantage of embedding session into context is that you can combine more than one method calls in one session, like:</div><div><br></div><div>model.py</div><div>def complex_operation(context):</div><div>    policy_check(context)</div><div>    with context.session.begin():</div><div>        core.operation1(context)</div><div>        core.operation2(context)</div><div><br></div><div>But this approach moves session handling from core module to models module and core module just provides some utility methods.</div><div><br></div><div>I'm not sure which one is better.</div><div><br></div><div>BR</div><div>Zhiyuan</div></div>