# Shared dependencies

**URL:** https://discourse.nameko.io/t/shared-dependencies/121
**Category:** googlegroup
**Created:** [February 15, 2016, 10:33pm UTC](https://discourse.nameko.io/t/shared-dependencies/121 "2016-02-15T22:33:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Conor\_Seabrook](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/conor_seabrook/32/85_2.png) [@Conor\_Seabrook](https://discourse.nameko.io/u/Conor_Seabrook)
#### Post date: [February 15, 2016, 10:33pm UTC](https://discourse.nameko.io/t/shared-dependencies/121/1 "2016-02-15T22:33:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![David\_Szotten](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/david_szotten/32/6_2.png) [@David\_Szotten](https://discourse.nameko.io/u/David_Szotten)
#### Post date: [February 16, 2016, 11:37am UTC](https://discourse.nameko.io/t/shared-dependencies/121/2 "2016-02-16T11:37:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Conor\_Seabrook](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/conor_seabrook/32/85_2.png) [@Conor\_Seabrook](https://discourse.nameko.io/u/Conor_Seabrook)
#### Post date: [February 16, 2016, 1:04pm UTC](https://discourse.nameko.io/t/shared-dependencies/121/3 "2016-02-16T13:04:05Z")

</div>

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):  
&nbsp;&nbsp;&nbsp;&nbsp;name = "account"

&nbsp;&nbsp;&nbsp;&nbsp;sqlalchemy\_dependency = Session(Base)

&nbsp;&nbsp;&nbsp;&nbsp;account\_setup\_dependency = AccountSetupDependency()

&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def create\_account(self, someinfo):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_account\_id =  
self.account\_setup\_dependency.create\_new\_account(someinfo)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_account\_id

&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def create\_account\_2(self, someinfo):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_account\_id =  
self.account\_setup\_dependency.create\_new\_account\_2(self.sqlalchemy\_dependency,  
someinfo)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_account\_id

# account\_setup\_dependency.py

from nameko.extensions import DependencyProvider

class AccountSetupDependency(DependencyProvider):  
&nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass

&nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self

&nbsp;&nbsp;&nbsp;&nbsp;def create\_new\_account(self, someinfo):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# What I'd like to do, correctly init the sqlalchemy dependency

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# sql\_alchemy = self.container.dependencies.?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_id = ...

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do some db work  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc = PlayerAccount(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someinfo=someinfo,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_date=datetime.datetime.utcnow()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.add(acc)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.commit()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_id

&nbsp;&nbsp;&nbsp;&nbsp;def create\_new\_account\_2(self, sqlalchemy\_ref, someinfo):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# What I'm doing now, passing in sqlalchemy reference

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_id = ...

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do some db work  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc = PlayerAccount(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someinfo=someinfo,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_date=datetime.datetime.utcnow()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.add(acc)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.commit()

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

### Author: ![David\_Szotten](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/david_szotten/32/6_2.png) [@David\_Szotten](https://discourse.nameko.io/u/David_Szotten)
#### Post date: [February 16, 2016, 1:47pm UTC](https://discourse.nameko.io/t/shared-dependencies/121/4 "2016-02-16T13:47:27Z")

</div>

Hi,

I understand what you are trying to do now, though i don't think it's the  
right approach. Dependencies are meant for interactions with the outside  
world, and not really for business logic. For your case, i would just use a  
shared helper, (could be a bare function), and pass any dependencies into  
it.

# this can now be tested without any nameko machinery,  
# just needs a sqla session  
def create\_new\_account(session, someinfo):  
&nbsp;&nbsp;&nbsp;&nbsp;new\_id = ...

&nbsp;&nbsp;&nbsp;&nbsp;# do some db work  
&nbsp;&nbsp;&nbsp;&nbsp;acc = PlayerAccount(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someinfo=someinfo,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_date=datetime.datetime.utcnow()  
&nbsp;&nbsp;&nbsp;&nbsp;)  
&nbsp;&nbsp;&nbsp;&nbsp;session.add(acc)  
&nbsp;&nbsp;&nbsp;&nbsp;session.commit()

&nbsp;&nbsp;&nbsp;&nbsp;return new\_id

class MyService(object):  
&nbsp;&nbsp;&nbsp;&nbsp;name = "account"

&nbsp;&nbsp;&nbsp;&nbsp;# once injected, (e.g. when rpc methods are executing and we are using  
&nbsp;&nbsp;&nbsp;&nbsp;# this) it's just a sqlalchemy session, so i prefer the name to reflect  
&nbsp;&nbsp;&nbsp;&nbsp;# that (i.e. not using the "\_dependency" suffix)  
&nbsp;&nbsp;&nbsp;&nbsp;sqla\_session = Session(Base) # or just `session`

&nbsp;&nbsp;&nbsp;&nbsp;@rpc  
&nbsp;&nbsp;&nbsp;&nbsp;def create\_account(self, someinfo):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_account\_id = create\_new\_account(self.sqla\_session, someinfo)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_account\_id

Regards,  
David

> **···**
>
> On Tuesday, 16 February 2016 13:04:05 UTC, conor.s...@gmail.com wrote:
> 
> > 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):  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = "account"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;sqlalchemy\_dependency = Session(Base)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;account\_setup\_dependency = AccountSetupDependency()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def create\_account(self, someinfo):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_account\_id =  
> > self.account\_setup\_dependency.create\_new\_account(someinfo)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_account\_id
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def create\_account\_2(self, someinfo):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_account\_id =  
> > self.account\_setup\_dependency.create\_new\_account\_2(self.sqlalchemy\_dependency,  
> > someinfo)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_account\_id
> > 
> > # account\_setup\_dependency.py
> > 
> > from nameko.extensions import DependencyProvider
> > 
> > class AccountSetupDependency(DependencyProvider):  
> > &nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def create\_new\_account(self, someinfo):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# What I'd like to do, correctly init the sqlalchemy dependency
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# sql\_alchemy = self.container.dependencies.?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_id = ...
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do some db work  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc = PlayerAccount(  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someinfo=someinfo,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_date=datetime.datetime.utcnow()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.add(acc)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.commit()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_id
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def create\_new\_account\_2(self, sqlalchemy\_ref, someinfo):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# What I'm doing now, passing in sqlalchemy reference
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_id = ...
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do some db work  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc = PlayerAccount(  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someinfo=someinfo,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_date=datetime.datetime.utcnow()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.add(acc)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.commit()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

### Author: ![Conor\_Seabrook](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/conor_seabrook/32/85_2.png) [@Conor\_Seabrook](https://discourse.nameko.io/u/Conor_Seabrook)
#### Post date: [February 16, 2016, 2:32pm UTC](https://discourse.nameko.io/t/shared-dependencies/121/5 "2016-02-16T14:32:34Z")

</div>

Ah ok, that makes sense.

Thanks again for your insights, much appreciated!

> **···**
>
> On Tuesday, February 16, 2016 at 8:47:27 AM UTC-5, David Szotten wrote:
> 
> > Hi,
> > 
> > I understand what you are trying to do now, though i don't think it's the  
> > right approach. Dependencies are meant for interactions with the outside  
> > world, and not really for business logic. For your case, i would just use a  
> > shared helper, (could be a bare function), and pass any dependencies into  
> > it.
> > 
> > # this can now be tested without any nameko machinery,  
> > # just needs a sqla session  
> > def create\_new\_account(session, someinfo):  
> > &nbsp;&nbsp;&nbsp;&nbsp;new\_id = ...
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;# do some db work  
> > &nbsp;&nbsp;&nbsp;&nbsp;acc = PlayerAccount(  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someinfo=someinfo,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_date=datetime.datetime.utcnow()  
> > &nbsp;&nbsp;&nbsp;&nbsp;)  
> > &nbsp;&nbsp;&nbsp;&nbsp;session.add(acc)  
> > &nbsp;&nbsp;&nbsp;&nbsp;session.commit()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;return new\_id
> > 
> > class MyService(object):  
> > &nbsp;&nbsp;&nbsp;&nbsp;name = "account"
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;# once injected, (e.g. when rpc methods are executing and we are using  
> > &nbsp;&nbsp;&nbsp;&nbsp;# this) it's just a sqlalchemy session, so i prefer the name to  
> > reflect  
> > &nbsp;&nbsp;&nbsp;&nbsp;# that (i.e. not using the "\_dependency" suffix)  
> > &nbsp;&nbsp;&nbsp;&nbsp;sqla\_session = Session(Base) # or just `session`
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > &nbsp;&nbsp;&nbsp;&nbsp;def create\_account(self, someinfo):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_account\_id = create\_new\_account(self.sqla\_session, someinfo)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_account\_id
> > 
> > Regards,  
> > David
> > 
> > On Tuesday, 16 February 2016 13:04:05 UTC, conor.s...@gmail.com wrote:
> > 
> > > 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):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;name = "account"
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;sqlalchemy\_dependency = Session(Base)
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;account\_setup\_dependency = AccountSetupDependency()
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def create\_account(self, someinfo):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_account\_id =  
> > > self.account\_setup\_dependency.create\_new\_account(someinfo)
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_account\_id
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;@rpc  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def create\_account\_2(self, someinfo):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_account\_id =  
> > > self.account\_setup\_dependency.create\_new\_account\_2(self.sqlalchemy\_dependency,  
> > > someinfo)
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_account\_id
> > > 
> > > # account\_setup\_dependency.py
> > > 
> > > from nameko.extensions import DependencyProvider
> > > 
> > > class AccountSetupDependency(DependencyProvider):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;def setup(self):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pass
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def get\_dependency(self, worker\_ctx):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def create\_new\_account(self, someinfo):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# What I'd like to do, correctly init the sqlalchemy dependency
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# sql\_alchemy = self.container.dependencies.?  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_id = ...
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do some db work  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc = PlayerAccount(  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someinfo=someinfo,  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_date=datetime.datetime.utcnow()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.add(acc)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.commit()
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return new\_id
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;def create\_new\_account\_2(self, sqlalchemy\_ref, someinfo):  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# What I'm doing now, passing in sqlalchemy reference
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_id = ...
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do some db work  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc = PlayerAccount(  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;someinfo=someinfo,  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;create\_date=datetime.datetime.utcnow()  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.add(acc)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.sqlalchemy\_ref.commit()
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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
