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)

Introduction Available since v0.11Browser

The @aedart/container package offers an adaptation of Laravel's Service Container (originally licensed under MIT).

The tools provided by this package give you a way to:

  • Manage class dependencies
  • Perform dependency injection

Example

Bindings

Imagine that you have an Api client (or any component for that matter). Whenever it is needed, you want it to be injected into components that depend on it.

export default class ApiClient
{
    // ...implementation not shown...
}

To ensure that dependency injection can be performed, you must first bind the component in the service container. Each binding requires a unique identifier, e.g. a string, symbol, number...etc.

import { Container } from "@aedart/container";
import { ApiClient } from "@acme/api";

const container = Container.getInstance();

// Bind 'my_api_client' to the ApiClient component...
container.bind('my_api_client', ApiClient);

Define Dependencies

To define the dependencies of a component, use the dependencies() decorator. By itself, the decorator does not do anything more than to associate a component with one or more dependencies (binding identifiers). In other words, the decorator does not automatically inject anything into your class. It only registers the dependencies as metadata onto a class.

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

@dependencies('my_api_client')
export default class BookService
{
    apiClient;
    
    constructor(client) {
        this.apiClient = client;
    }
    
    // ...remaining not shown...
}

Resolve

When you want to resolve a component, with all of its dependencies injected into it, use the service container's make() method.

import { Container } from "@aedart/container";
import { BookService } from "@acme/app/services";

const bookService = Container.getInstance().make(BookService);

console.log(bookService.apiClient); // ApiClient

Onward

The above shown example illustrates the most basic usage of the service container. Throughout the remaining of this package's documentation, more examples and use-cases are covered.

Edit page
Last Updated:
Contributors: alin
Next
Prerequisites