Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Core container as singletone for entire app #557

Unanswered
VasyaGaikin asked this question in Q&A
Discussion options

App Package (Container) Diagramm

The CoreContainer container contains:

class CoreContainer( containers.DeclarativeContainer ):
 arguments = providers.Resource( parse_arguments )
 config = providers.Resource( parse_config, arguments=arguments )
 _logging = providers.Resource( init_logging,
 arguments=arguments,
 config=config )
 logger_factory = providers.Factory( logging.getLogger ).provider

The main question is about CoreContainer. Is there a way to make it
share for the entire application? The only way I found is when
importing top-level containers (CleanContainer, DatabaseInitContainer,
ParseContainer, ...) specify the CoreContainer as a
dependency and pass it into the child containers.

when I do this:

class DatabaseContainer( containers.DeclarativeContainer ):
 core: CoreContainer = providers.Container( CoreContainer )
 ...
class DownloadContainer( containers.DeclarativeContainer ):
 core: CoreContainer = providers.Container( CoreContainer )
 ...
class ParseContainer( containers.DeclarativeContainer ):
 core: CoreContainer = providers.Container( CoreContainer )

then all elements in CoreContainer are initialized multiple times.
It was it would be convenient if the container itself was like a
Singletone for the entire application.

You must be logged in to vote

Replies: 1 comment

Comment options

This looks like a good use case for providers.DependenciesContainer
Maybe something like this:

class DatabaseContainer( containers.DeclarativeContainer ):
 core: CoreContainer = providers.DependenciesContainer( CoreContainer )
 ...
class DownloadContainer( containers.DeclarativeContainer ):
 core: CoreContainer = providers.DependenciesContainer( CoreContainer )
 ...
class ParseContainer( containers.DeclarativeContainer ):
 core: CoreContainer = providers.CoreContainer( CoreContainer )

Then constructing the containers would look something like this:

core = CoreContainer()
database = DatabaseContainer(core=core)
download = DownloadContainer(core=core)
parse = ParseContainer(core=core)

This would only require one instance of the CoreContainer. You may have to massage this example code a bit before it works, but I think this is the problem DependenciesContainer is designed to solve.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /