feat(container): add conditional bindings - #70
Open
RomainLanz wants to merge 2 commits into
Open
Conversation
Replaces "bindWhen" with a fluent builder, so conditional bindings read
like the rest of the container API and support singletons.
container.if(condition).bind(PaymentGateway, resolver)
container.if(condition).singleton(SearchService, resolver)
The "bindWhen" name collided with the existing "when" method used for
contextual bindings, where "when" refers to the parent class rather
than a predicate.
Changes:
- Extract the binding entry shape into "BindingEntry" and reuse it for
conditional bindings, so both share a single resolution helper inside
the resolver. Conditional bindings now get the same singleton caching
and run-hooks-once behavior as regular bindings, instead of
re-implementing hooks and events.
- Pass the parent asking for the binding to the condition as a third
argument. Conditions can now combine a predicate with the calling
class. The parent is null when resolving directly via "make".
- Attach a help message when a binding only has conditional bindings
registered and none of their conditions match. Without it, the error
reads like the binding was never registered.
- Make "conditionalBindings" optional on the ContainerResolver
constructor, so constructing a resolver directly is not a breaking
change.
- Document the full resolution order, the condition arguments, the need
to guard conditions that read request specific values, and that
"hasBinding" reports registration rather than resolvability.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey! 👋🏻
This PR adds a new API to register conditional bindings.
Example Story: You want to try a new payment gateway and roll-out that feature only to a few users.
With the new API, you can register a conditional bindings that resolve a different class depending on a condition.