ivankorobkov / python-inject

Python dependency injection
Apache License 2.0
672 stars 77 forks source link

Django: inject.InjectorException: Injector is already configured (proposal) #11

Closed alefnula closed 9 years ago

alefnula commented 9 years ago

Since django is (unfortunately) loading some of its modules more than once, inject is throwing an: inject.InjectorException: Injector is already configured exception.

My current workaround is:

if inject.get_injector() is None:
    inject.configure(ioc.production)

But that's exposing the internals of the inject library. I think that it would be cleaner to have something like:

if not inject.is_configured():
    inject.configure(ioc.production)

But, if you're OK with the "hacky" solution, I'll close the issue.

Thank you for your time :)

ivankorobkov commented 9 years ago

As far as I remember, Django way of loading modules is rather weird. It does not have a well-defined entry point where you can put an injector initialization.

I would do the same as you:

if not inject.is_configured():
    inject.configure(ioc.production)

But I do not Django internals, may be, there is a better way.

ivankorobkov commented 9 years ago

I will upload a new version to pypi in 5 mins.

ivankorobkov commented 9 years ago

I have uploaded a new version to PyPi https://pypi.python.org/pypi/Inject/3.2.0

I added is_configured and configure_once. The latter can be called multiple times to configure an injector. It also eliminates race conditions if you try to configure an injector multiple times.

Thanks for an issue.

alefnula commented 9 years ago

Wow you're awesome! This was fast! Thanks a lot! :)