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)

Dependencies

In order for the Service Container to be able to automatically inject dependencies, when resolving components, you must first define them on a target class. The dependencies() decorator is used for this purpose.

import { dependencies } from "@aedart/support/container";

@dependencies('engine')
export default class Car
{
    engine = undefined;
    
    constructor(engine) {
        this.engine = engine;
    }
}

No automatic injection

The dependencies() decorator does not automatically inject anything into your class. It will only associate binding identifiers with the target class, as metadata. This means that you can instantiate a new instance of the class, without any side effects (dependencies must be manually given as arguments to the target class).

const car = new Car();
console.log(car.engine); // undefined

The Service Container's make() method is responsible for reading the defined dependencies, resolve them, and inject them into the target class.

Multiple Dependencies

The dependencies() decorator accepts an arbitrary amount of binding identifiers. This allows you to define multiple dependencies in a single call.

@dependencies(
    'warehouse_manager',
    'api_client',
    'events'
)
export default class Warehouse
{
    manager = undefined;
    apiClient = undefined;
    eventDispatcher = undefined;
    
    constructor(manager, apiClient, dispatcher) {
        this.manager = manager;
        this.apiClient = apiClient;
        this.eventDispatcher = dispatcher;
    }
}
Edit page
Last Updated:
Contributors: alin
Prev
Bindings
Next
Resolving