IonIon
Packages
  • next
  • current
Changelog
GitHub
Packages
  • next
  • current
Changelog
GitHub
  • Version 0.x

    • Release Notes
    • Upgrade Guide
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • Packages

    • Introduction
    • Container

      • Introduction
      • Prerequisites
      • How to install
      • Container Instance
      • Bindings
      • Dependencies
      • Resolving
      • Contextual Bindings
    • Contracts

      • Introduction
      • How to install
    • Support

      • Introduction
      • How to install
      • Arrays

        • About Arrays
        • Includes All
        • Includes Any
        • Is Array Like
        • Is Concat Spreadable
        • Is Safe Array Like
        • Is Typed Array
        • Merge
      • Concerns

        • About Concerns
        • Prerequisites
        • Concern Class
        • Using Concerns
        • Aliases
        • Conflict Resolution
        • Booting
        • Hooks
        • Edge Cases
        • JSDoc
      • Exceptions

        • About Exceptions
        • Configure Custom Error
        • Configure Stack Trace
        • Get Error Message
        • Custom Errors
      • Facades

        • About Facades
      • Meta

        • About Meta
        • Prerequisites
        • Supported Elements
        • Set & Get
        • Inheritance
        • Outside Changes
        • TC39 Proposal
        • Target Meta
      • Mixins

        • About Mixins
        • New Mixin
        • Apply Mixins
        • Instanceof
        • Inheritance
        • Onward
      • Object

        • About Objects
        • Forget
        • Forget All
        • Get
        • Has
        • Has All
        • Has Any
        • Has Unique ID
        • Is Cloneable
        • Is Populatable
        • Isset
        • Merge
        • Populate
        • Set
        • Unique ID
      • Reflections

        • About reflections
        • Assert Has Prototype Prop.
        • Class Looks Like
        • Class Own Keys
        • Get All Parents Of Class
        • Get Class Prop. Descriptor
        • Get Class Prop. Descriptors
        • Get Constructor Name
        • Get Name Or Desc. Tag
        • Get Parent Of Class
        • Has All Methods
        • Has Method
        • Has Prototype Property
        • Is Callable
        • Is Class Constructor
        • Is Class Method Reference
        • Is Constructor
        • Is Key Safe
        • Is Key Unsafe
        • Is Method
        • Is Subclass
        • Is Subclass Or Looks Like
        • Is WeakKind
      • Misc

        • About Misc.
        • Desc. Tag
        • Empty
        • Is Key
        • Is Primitive
        • Is Property Key
        • Isset
        • Merge Keys
        • To Weak Ref.
      • Callback Wrapper
    • Vuepress Utils

      • Introduction
      • How to install
      • Navigation

        • Archive
      • Plugins

        • Last Updated
      • Components

        • Version Disclaimer
    • XYZ (test package)

Contextual Bindings

In situations when multiple classes make use of the same dependency, but you wish to inject a different component or value on some of those classes, then you can make use of "context binding". The when() method allows you to specify (overwrite) the implementation to be resolved and injected, for a given target class.

container.when(ApiService)
        .needs('storage')
        .give(CookieStorage);

container.when(UsersRepository, BooksRepository)
        .needs('api_client')
        .give(() => {
           return new AcmeApiClient(); 
        });

To illustrate the usefulness of contextual binding a bit further, consider the following example:

@dependency('storage')
class A {
    // ...not shown...
}

@dependency('storage')
class B {
    // ...not shown...
}

@dependency('storage')
class C {
    // ...not shown...
}

@dependency('storage')
class D {
    // ...not shown...
}

// Register "default" storage binding
container.singleton('storage', CookieStorage);

// Register contextual binding for C and D
container.when(C, D)
    .needs('storage')
    .give(() => {
        return new CloudStorage('s3');
    });

In the above shown example, all classes define the same binding identifier ("storage") as a dependency. By default, a "storage" binding is registered in the Service Container, which ensures that when the classes are resolved, a CookieStorage component instance is injected into each target class instance.

However, classes C and D require a different implementation, than the one offered by the "storage" binding. To achieve this, and without overwriting the default "storage" binding, a new contextual binding is registered that affects only classes C and D. When they are resolved, a different implementation of injected into the target classes.

const c = container.make(C);
console.log(c.storage); // CloudStorage
Edit page
Last Updated:
Contributors: alin
Prev
Resolving