Finistere / antidote

Dependency injection for Python
MIT License
90 stars 9 forks source link

How to define specific implementation in catalogue based on conditions? #75

Closed gnkow closed 1 year ago

gnkow commented 1 year ago

Is there any similar class to providers.Selector from dependency_injector? I want have different implementations injected based on antidote constants values (env variables).

gnkow commented 1 year ago

from antidote import const, inject, interface, implements, world CLOUD = const('AWS') # some configuration loader somewhere @inject def on_cloud(expected: str, actual: str = inject[CLOUD]) -> bool: return expected == actual @interface class CloudAPI: pass @implements(CloudAPI).when(on_cloud('GCP')) class GCPapi(CloudAPI): pass @implements(CloudAPI).when(on_cloud('AWS')) class AWSapi(CloudAPI): pass world[CloudAPI] <AWSapi object ...>

Found it in docs. I would say this should be more visible on user guide as is a common user request. Ty, amazing package.