Skip to content

Contextual binding? #187

@michael-rubel

Description

@michael-rubel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions