Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind a provider to multiple interfaces? #205

Open
bo-at-pleno opened this issue Sep 9, 2022 · 0 comments
Open

Bind a provider to multiple interfaces? #205

bo-at-pleno opened this issue Sep 9, 2022 · 0 comments

Comments

@bo-at-pleno
Copy link

bo-at-pleno commented Sep 9, 2022

Hello Alec & open source contributors - I love this library - it feels very well designed and strikes a sweet spot between complexity / ease of use. However, I am running into a little bit of a snag and was wondering if anyone else came across this problem:

I currently have some helper provider modules that specify how to provide for a certain interface type (this is defined as a class member on modules that I wanted to bind DI to):

class _ModuleProvider(Module):
    @singleton
    @provider
    def provide_base(self, context : Context, config: Configuration) -> BaseType:
        return DerivedType(context, **config)

I dynamically load all modules and then add these to the provider container:

provider_containers = [config_provider]

# register all providers
for component in component_classes:
    # all modules should implement this provider class
    assert hasattr(
        component, "_ModuleProvider"
    ), "Component {} does not define a provider".format(component.__name__)
    provider_containers.append(component._ModuleProvider())

# later on
injector = Injector(provider_containers, auto_bind=False) # note I disabled autobind here
pipeline = injector.get(Pipeline)

However, some Pipeline implementations might have stricter requirements on the interface type so their constructor might take a DerivedType directly.

I currently work around this by declaring another provider

class _ModuleProvider(Module):
    @singleton
    @provider
    def provide_base(self, context : Context, config: Configuration) -> BaseType:
        return DerivedType(context, **config)

    @singleton
    @provider
    def provide_derived(self, context : Context, config: Configuration) -> DerivedType:
        return DerivedType(context, **config)

But I was just wondering if it's possible to do this:

class _ModuleProvider(Module):
    @singleton
    @provider
    def provide_derived(self, context : Context, config: Configuration) -> Union[BaseType, DerivedType]:
        return DerivedType(context, **config)

If there's a better way than what I was doing, I'd appreciate it too. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant