ets-labs / python-dependency-injector

Dependency injection framework for Python
https://python-dependency-injector.ets-labs.org/
BSD 3-Clause "New" or "Revised" License
3.83k stars 304 forks source link

Provide different kind of service implement if some condition is met or not #329

Closed MadJlzz closed 3 years ago

MadJlzz commented 3 years ago

Hello,

I've searched through you're documentation but it seems not be possible at the moment to provide specific implementation of a service whenever we reach a certain condition with a value coming from a property file.

What I am intending to do with that is something like:

Hey, is there a properties in your .ini file set to this value ? Okay, then service implementation is A, otherwise it should be B

Here's what I am trying to do: https://gist.github.com/MadJlzz/22f977a6fd91e48d3c120dd9de9510f9

I know it is not working but I guess you will understand what I've tried to do. I've also understand that providers.Configuration is to load properties from a file and inject those inside one of the dependencies we are wishing to create. (so bad use in my example)

If not possible at the moment, it could be a nice feature, no?

rmk135 commented 3 years ago

Hey @MadJlzz ,

Did you look at Selector provider https://python-dependency-injector.ets-labs.org/providers/selector.html? Here is a container from your gist:

class Container(containers.DeclarativeContainer):
    configuration = providers.Configuration()

    if configuration.profile == "anthos":
        stream = providers.Singleton(
            PostgreSQLStream
        )
    else:
        stream = providers.Singleton(
            BigQueryStream
        )

With the Selector provider it will look like this:

class Container(containers.DeclarativeContainer):
    configuration = providers.Configuration()

    stream = providers.Selector(
        configuration.profile,
        anthos=providers.Singleton(PostgreSQLStream),
        bigquery=providers.Singleton(BigQueryStream),
    )
MadJlzz commented 3 years ago

This exactly what I wanted! What about using this question to add an example in the documentation? (in the example category I mean) I can do it!

rmk135 commented 3 years ago

What about using this question to add an example in the documentation? (in the example category I mean)

Do you mean example with the Selector provider?

rmk135 commented 3 years ago

This exactly what I wanted!

Ok, very good :)

rmk135 commented 3 years ago

There is an example with the Selector here https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/movie-lister.

There is also a tutorial showing how to build it step-by-step https://python-dependency-injector.ets-labs.org/tutorials/cli.html

MadJlzz commented 3 years ago

Okay, I guess I really need to clean my glasses before creating an issue. Thanks a lot for the support!

rmk135 commented 3 years ago

No problems, you're welcome.

Most of the examples and providers take their origin from the issues like that. Thanks for raising that one. That's not a bad idea to have an example with Selector provider better promoted.

Feel free to open another one if you see something not working, or not clear, or you just need an advice.