Shared dependencies

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

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

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

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):
    new_id = ...

    # do some db work
    acc = PlayerAccount(
            someinfo=someinfo,
            create_date=datetime.datetime.utcnow()
    )
    session.add(acc)
    session.commit()

    return new_id

class MyService(object):
    name = "account"

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

    @rpc
    def create_account(self, someinfo):
        new_account_id = create_new_account(self.sqla_session, someinfo)

        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):
    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

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):
    new_id = ...

    # do some db work
    acc = PlayerAccount(
            someinfo=someinfo,
            create_date=datetime.datetime.utcnow()
    )
    session.add(acc)
    session.commit()

    return new_id

class MyService(object):
    name = "account"

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

    @rpc
    def create_account(self, someinfo):
        new_account_id = create_new_account(self.sqla_session, someinfo)

        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):
    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