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

Inject multiple implementations of an interface with Type based injection #13

Closed
stortiz-lifeworks opened this issue Jun 9, 2021 · 3 comments
Labels
enhancement New feature or request
Milestone

Comments

@stortiz-lifeworks
Copy link

A very useful feature when using Type based injection would be the ability to inject all the implementations for a given interface as a list.

Here is an example of this same feature from the Java Spring documentation:
https://docs.spring.io/spring-framework/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#beans-autowired-annotation
Screenshot 2021-06-09 at 15 03 56

The way it would be used would be as follows:

class IMyService:
    def my_function(self, val: str) -> str:
        ...


@inject(alias=IMyService)
class FirstService(IMyService):
    def my_function(self, val: str) -> str:
        return f'First {val}'


@inject(alias=IMyService)
class SecondService(IMyService):
    def my_function(self, val: str) -> str:
        return f'Second {val}'

@inject
def list_vals(services: List[IMyService]) -> List[str]:
    return [service.my_function() for service in services]

In the snippet above, the parameter services of list_vals would receive the objects of both classes implementing IMyService.

@dkraczkowski
Copy link
Contributor

@stortiz-lifeworks Have a look here: #14
Had to drop python3.6 support as I am short on time and actually lifecycle of 3.6 ends in december so I dont think I care anymore about such an old version.

@stortiz-lifeworks
Copy link
Author

stortiz-lifeworks commented Jun 14, 2021

I have tested it and that is the requested feature.

But in the example added to the documentation there is a "register=False" argument passed to the decorator that is raising an exception when used, and that does not appear in the documentation.

Thanks.

@dkraczkowski
Copy link
Contributor

@stortiz-lifeworks Available in 0.6.0 - https://github.com/kodemore/kink/actions/runs/935773769
Closing the issue now.

@dkraczkowski dkraczkowski added this to the 0.6.0 milestone Jun 14, 2021
@dkraczkowski dkraczkowski added the enhancement New feature or request label Jun 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants