JSPM

@webdocgroup/container

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q28416F
  • License MIT

A lightweight, type-safe dependency injection container for TypeScript.

Package Exports

  • @webdocgroup/container

Readme

Container

Webdoc container

The Webdoc service container is a simple tool for managing class dependencies and performing dependency injection.

Installation

npm install @webdocgroup/container

Usage

Simple Bindings

Use bind to register a factory function. The factory is called every time you resolve the key, returning a new instance each time.

type Application = {
    'command.createUser': CreateUserHandler;
    'query.getUser': GetUserQueryHandler;
    userRepository: UserRepository;
};

const container = new Container<Application>();

// Register a factory for 'command.createUser'.
// Each call to resolve will create a new instance.
container.bind('command.createUser', () => new CreateUserHandler());

// Resolving returns a new instance every time.
const handler = container.resolve('command.createUser');

Binding Singletons

Use singleton to register a factory that is only called once. All future resolves return the same instance.

type Application = {
    'command.createUser': CreateUserHandler;
    'query.getUser': GetUserQueryHandler;
    userRepository: UserRepository;
};

const container = new Container<Application>();

// Register a singleton factory for 'userRepository'.
// Only one instance will ever be created and reused.
container.singleton('userRepository', () => new UserRepository());

// Factories can resolve other dependencies from the container.
container.bind(
    'query.getUser',
    () =>
        new GetUserHandler({
            userRepository: container.resolve('userRepository'),
        })
);