ivankorobkov / python-inject

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

How to inject dependencies that have dependencies #63

Closed caiquecp closed 4 years ago

caiquecp commented 4 years ago

What I'm trying to do:

def config_inject(binder: inject.Binder) -> None:
        binder.bind(Database, MongoDatabase())
        binder.bind(SomeRepository, MongoSomeRepository())
    inject.configure(config_inject)

MongoSomeRepository has dependency over Database, but when I'm trying to config it this way I got the error "No injector is configured". I guess it happens because at the moment the bind is being made the injector is not ready. So how do I handle this case?

ivankorobkov commented 4 years ago

Hi,

Use ‘binder.bind_to_constructor(Database, MongoDatabase)’.

‘MongoDatabase()’ inside ‘config_injector’ tries to get its dependencies and accesses the global injector which is not configured yet.

caiquecp commented 4 years ago

OK, I'll do it. Thank you!