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

Contextual binding? #187

Open
michael-rubel opened this issue Oct 18, 2024 · 1 comment
Open

Contextual binding? #187

michael-rubel opened this issue Oct 18, 2024 · 1 comment

Comments

@michael-rubel
Copy link

Let's say I have an AbstractFormatter class.

I register the base binding:

self.container.register(AbstractFormatter, JsonFormatter)
class Processor:
    def __init__(
        self,
        json_formatter: AbstractFormatter,
    ):
        self.json_formatter = json_formatter

But one specific class wants another implementation by the same contract:

class CustomProcessor:
    def __init__(
        self,
        markdown_formatter: AbstractFormatter,
    ):
        self.markdown_formatter = markdown_formatter

Now, I want to do the following:

(self.container.when(CustomProcessor)
               .wants(AbstractFormatter)
               .give(MarkdownFormatter))

I.e. only CustomProcessor should receive the markdown formatter.

P.S. Maybe there are other ways to do the same? Inspired by this.

@bobthemighty
Copy link
Owner

If it makes you feel any better, btw, this one is uncontroversial and - I think - related to #35 .

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

2 participants