Hi David,
Your continued assistance is much appreciated.
So, I'm trying to hide some complexity inside a dependency. Where I'm
getting tripped up is the correct pattern when accessing another
dependency. Say for example a database dependency like redis or sql alchemy.
Let's say I have an account service which has a fair amount of business
logic. I'd like to create a number of dependencies which house this
business logic, but they need access to other dependencies for database
access etc.
So for example:
# service.py
from nameko.rpc import rpc
from sqlalchemy.ext.declarative import declarative_base
from account_setup import AccountSetupDependency
from nameko_sqlalchemy import Session
Base = declarative_base()
class AccountService(object):
name = "account"
sqlalchemy_dependency = Session(Base)
account_setup_dependency = AccountSetupDependency()
@rpc
def create_account(self, someinfo):
new_account_id =
self.account_setup_dependency.create_new_account(someinfo)
return new_account_id
@rpc
def create_account_2(self, someinfo):
new_account_id =
self.account_setup_dependency.create_new_account_2(self.sqlalchemy_dependency,
someinfo)
return new_account_id
# account_setup_dependency.py
from nameko.extensions import DependencyProvider
class AccountSetupDependency(DependencyProvider):
def setup(self):
pass
def get_dependency(self, worker_ctx):
return self
def create_new_account(self, someinfo):
# What I'd like to do, correctly init the sqlalchemy dependency
# sql_alchemy = self.container.dependencies.?
new_id = ...
# do some db work
acc = PlayerAccount(
someinfo=someinfo,
create_date=datetime.datetime.utcnow()
)
self.sqlalchemy_ref.add(acc)
self.sqlalchemy_ref.commit()
return new_id
def create_new_account_2(self, sqlalchemy_ref, someinfo):
# What I'm doing now, passing in sqlalchemy reference
new_id = ...
# do some db work
acc = PlayerAccount(
someinfo=someinfo,
create_date=datetime.datetime.utcnow()
)
self.sqlalchemy_ref.add(acc)
self.sqlalchemy_ref.commit()
return new_id
Hopefully that make sense. Happy to provide more info.
Regards,
Conor
···
On Tuesday, February 16, 2016 at 6:37:32 AM UTC-5, David Szotten wrote:
Hi Conor,
Could you provide a little more info, explaining what you are trying to
do, and maybe even a stripped down code example, to help me better
understand?
Best,
David
On Monday, 15 February 2016 22:33:26 UTC, conor.s...@gmail.com wrote:
Hello,
I'm creating a dependency and need to access the sqlalchemy dependency
from the one I'm creating. What is the "Nameko" way of doing this? There
didn't seem to be a real easy way to do this other then passing a reference
in to the method from the rpc call.
Thanks,
Conor