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 Dependency Clarification #29

Open
n0nag0n opened this issue Feb 16, 2023 · 3 comments
Open

Contextual Dependency Clarification #29

n0nag0n opened this issue Feb 16, 2023 · 3 comments

Comments

@n0nag0n
Copy link
Contributor

n0nag0n commented Feb 16, 2023

Hey there,

Been thinking about how you guys do dependency injection and in your docs on this page https://makoframework.com/docs/9.0/getting-started:dependency-injection you mention contextual injection. It feels like it's important to know, but I don't think I quite understand what the goal of it is.
image
Is there a real life example that could better explain how it works (for me and others)?

@freost
Copy link
Member

freost commented Feb 17, 2023

It's probably the least used feature of the container but it was useful for us when we had two versions on an API while we migrated from Sphinx to Elastic. Both the endpoint controllers required an instance of SearchInterface. One got a SphinxSearch instance while the other got the ElasticSearch instance. It allowed us to run both versions in parallel until Sphinx was replaced.

@n0nag0n
Copy link
Contributor Author

n0nag0n commented Feb 17, 2023

So if I'm understanding your example correctly, would you run something like this:

$container->registerContextualDependency(Api_v1_Controller::class, SearchInterface::class, SphinxSearch::class);
$container->registerContextualDependency(Api_v2_Controller::class, SearchInterface::class, ElasticSearch::class);

@freost
Copy link
Member

freost commented Feb 17, 2023

Yeah that's right. It's probably not something you'll use often but it was nice to have when we needed it.

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