Description
Hi,
Thank you so much for a wonderful project. I am using dependency injection in a Django side project of mine.
I notice a big problem with the way the container initialize. As you state in the example section for Django, we initiate the container in __init__.py
at project level.
from .di_containers import DIContainer
from . import settings
di_container = DIContainer() -> create dependency before other app load
...
https://python-dependency-injector.ets-labs.org/examples/django.html
However, if one of your provider (Ex: UserService needs User model) requires model to do some works, django will throw django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
I am not so sure how to solve it? I am using Singleton in my container (required). Not sure if it that the reason?
class DIContainer(containers.DeclarativeContainer):
user_service = providers.Singleton(UserService)
from api_core.apps.user.models import User <- error cause by this import
@inject
class UserService:
def __init__(self):
self.test = 'test'
Update: I solve the problem with local import. But still I have to import locally multiple places in different function. The main root cause still because of initialization of the container in __init__.py
at project level. Let say we move it to a custom app, which register at the end of INSTALLED_APP list, we still have a problem how to wire them to the other app.
I don't know if you familiar with ReactiveX. I think this package can help solve the problem of wiring https://github.com/ReactiveX/RxPY. We could have an subscriber at each AppConfig in def ready: ...
. Then when we initiate the container, it will trigger a signal to tell them, ok it is safe for you guys to wire now ?