Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circularly interrelated packages #766

Open
Arseniy-Popov opened this issue Nov 15, 2023 · 1 comment
Open

Circularly interrelated packages #766

Arseniy-Popov opened this issue Nov 15, 2023 · 1 comment

Comments

@Arseniy-Popov
Copy link

When using subcontainers to organize layers in an application, there likely won't be circular dependencies between them. However, with domains this is not the case.

@dataclass
class UserService:
    payments: "PaymentsService"

    def user_action(self):
        print("user action")
        self.payments.payment_action


@dataclass
class PaymentsService:
    users: UserService

    def payment_action(self):
        print("payment action")


class UserContainer(containers.DeclarativeContainer):
    payments = providers.DependenciesContainer()

    service = providers.Factory(UserService, payments=payments.service)


class PaymentContainer(containers.DeclarativeContainer):
    users = providers.DependenciesContainer()

    service = providers.Factory(PaymentsService, users=users.service)



class ApplicationContainer(containers.DeclarativeContainer):
    users = providers.Container(UserContainer, payments=???)
    payments = providers.Container(PaymentContainer, users=users)

What's there to do if in the outermost container I want to link packages with circular dependencies between them?

@Arseniy-Popov Arseniy-Popov changed the title Interrelated packages Circularly interrelated packages Nov 15, 2023
@philipbjorge
Copy link

I'm not the maintainer, but let me share my experience using this library...

One of the explicit benefits of containers.DeclarativeContainer is that it prevents circular dependencies. You end up with a tree of dependencies instead of a cyclical graph (spaghetti).

In our application, our Users service actually has no dependencies, so I'm wondering if you might restructure your application in this way.

If you need circular dependencies for some reason (we've run into this with libraries on occasion), using the @inject style of injection can work in conjunction with string identifiers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants