1.1.4 • Published 7 years ago

typescript-simple-di v1.1.4

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Simple DI

A lean and simple Dependency Injection library for TypeScript

Install

npm install --save typescript-simple-di

Usage

First you need to register all classes/objects you want make available in your DI container

import { SimpleDI } from 'typescript-simple-di';

// register either classes or objects - name will automatically generated from the class name (e.g. the class UserService will become available as userservice)
SimpleDI.register(
  UserService,
  new Kong(process.env.KONG_API || 'http://kong.platform.local:8001'),
);

// or register with a custom name
SimpleDI.registerByName('my-user-service', new UserService());

To retreive the objects from your IoC container again you can either access them via the SimpleDI interface

// get an object by its name (will return type 'any')
const us = SimpleDI.get('userservice');
// or if you want it typed correctly
const usTyped = SimpleDI.get<UserService>('userservice');

// get an object by its type
const kong = SimpleDI.getByType(Kong);

Another way would be to use injection - therefore you need the Inject decorator

import { Inject } from 'typescript-simple-di';

You can annotate your property like that:

class MyClass {
  @Inject()
  private userService: UserService;

  @Inject('my-user-service')
  private myUs: UserService;
}

Note: this decorator only works for classes! If no name is specified in the Inject decorator it will fallback to the property type or if that's not possible the property name and inject the object with this name. So in this case above it will essentially assign/inject the result of SimpleDI.get('kong') to this class property.

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago