question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DeclarativeContainer of DeclarativeContainers

See original GitHub issue

As documentation shows “Bundles” and “Use cases” mini applications, we can prepare quite great containers with super clean dependencies. Declarative containers is my choice of keeping everything in place, that’s why I would like to see declarative containers even more usable.

Building whole application with its bundles and dependencies happens in functions. I would love to move that code to somewhat declarative containers too.

By doodling around a few hours I couldn’t figure it out how to archive this by Callables or Factories. So lets introduce some kind of providers.SubContainer.

Below there is naive example of bundle containers and application container which orchestrate whole project in declarative way.

# bundles/users/containers.py
from dependency_injector import containers, providers

from bundles.users.repositories import PGRepository
from bundles.users.commands import UserCommands


class PGAdapters(containers.DeclarativeContainer):
    user_repository = providers.Factory(PGRepositry)

class InMemoryAdapters(containers.DeclarativeContainer):
    user_repository = providers.Factory(InMemoryRepositry)

class Users(containers.DeclarativeContainer):
    adapters = providers.DependenciesContainer()

    commands = providers.Factory(
        UserCommands,
        user_repository=adapters.user_repository,
    )
# containers.py
from dependency_injector import containers, providers

from bundles import users, photos
from main import main


class Env1Adapters(containers.DeclarativeContainer):
    config = providers.Configuration('config')

    users_adapters = providers.SubContainer(users.PGAdapters)
    photos_adapters = providers.SubContainer(
        photos.S3Adapters,
        config=config,
    )

class Env2Adapters(containers.DeclarativeContainer):
    users_adapters = providers.SubContainer(users.InMemoryAdapters)
    photos_adapters = providers.SubContainer(photos.TmpDirAdapters)

class FooBarAdapters(containers.DeclarativeContainer):
    users_adapters = providers.SubContainer(users.FooAdapters)
    photos_adapters = providers.SubContainer(photos.BarAdapters)

class Application(containers.DeclarativeContainer):
    adapters = providers.DependenciesContainer()
    config = providers.Configuration('config')

    users = proviers.SubContainer(
        users.UserContainer,
        adapters=adapters.users_adapters,
    )
    photos = proviers.SubContainer(
        photos.PhotosContainer,
        adapters=adapters.photos_adapters,
    )

    main = providers.Callable(
        main,
        users=users,
        photos=photos,
    )
# run.py
from containers import Env1Adapters, Application

if __name__ == '__main__':
    config = {}

    adapters = Env1Adapters(config=config)  # or any other method to point adapters set
    application = Application(adapters=adapters, config=config)
    application.main()

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
approxitcommented, Sep 18, 2020

Great news, and works as a charm! Happy to have it implemented, even if your previous “work around” surprisingly obvious. 😃

1reaction
rmk135commented, Sep 1, 2020

UPDATE: There is a new provider for the mentioned case. It’s called the Container. The example can be found in tests - https://github.com/ets-labs/python-dependency-injector/blob/520945483f9b073bbe28aa3194bbb54a14cbdd86/tests/unit/providers/test_container_py2_py3.py#L40

Read more comments on GitHub >

github_iconTop Results From Across the Web

Declarative container - Dependency Injector
DeclarativeContainer is a class-based style of the providers definition. You create the declarative container subclass, put the providers as attributes and ...
Read more >
Dependency Injector — Python dependency injection framework
DeclarativeContainer ): config = providers. ... Using multiple declarative containers is a good choice for a large application.
Read more >
Charles Bay “Property-Based Declarative Containers”
A “ declarative container ” similarly presents this illusion of consistency, and simplicity: Properties are extracted to describe the contents ...
Read more >
Declarative containers | Docker for Serverless Applications
So what does a declarative container look like? It looks something like... Unlock full access. Continue reading with a subscription. Packt gives you ......
Read more >
Declarative containers lose ability to resolve host names over ...
I set up a few declarative containers on a NixOS 20.09 server I manage, and I find that over time, ... One declarative...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found