-
-
Notifications
You must be signed in to change notification settings - Fork 337
Core container as singletone for entire app #557
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
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.
Beta Was this translation helpful? Give feedback.