Socket
Socket
Sign inDemoInstall

di-typescript

Package Overview
Dependencies
2
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    di-typescript

A simple DI framework


Version published
Weekly downloads
6
increased by500%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Build Status

Dependency Injection

di-typescript is a simple dependency injection framework, which is build upon ts-di and angular/di.js.

Install

npm install di-typescript --save

Configuration

tsconfig.json file needs the following flags:

"experimentalDecorators": true,
"emitDecoratorMetadata": true

Usage

Using @Inject and create an instance

The following shows how to inject an instance of UserService into the constructor of App.

import {Inject, Injector} from 'di-typescript';

class UserService{}

@Inject
class App
{
  constructor(protected userService: UserService){}

}

const injector = new Injector();
// resolves UserService by creating an instance or retrieves it, when already exists
const app = injector.get(App); 

Using factories and tokens

If a dependency should not been resolved from a class but a function or a simple value, @useFactory or @useToken can be used like:

import {Inject, createToken, useToken, useFactory} from 'di-typescript';

interface IStorage { /* ... */ }
const storage: IStorage = { /* ... */ };
const storageFactory = () => storage;

interface IConfig { /* ... */ }
const configToken = createToken('app.config');

@Inject
class App
{
  constructor(protected userService: UserService,
              @useToken(configToken) protected config: IConfig,
              @useFactory(storageFactory) protected storage: IStorage){}

}

const config: IConfig = { /* ... */ };
const injector = new Injector([{provide: configToken, useValue: config}]);
const app = injector.get(App);

Testing and providing different values for specific tokens

When it comes to testing and further a mock should be injected than the actual service, this can be achieved as the following.


class UserService{}
class UserServiceMock{}

function storageMockFactory() {
  /* ... */
}

@Inject
class App
{
  constructor(protected userService: UserService,
              @useFactory(storageFactory) protected storage: IStorage){}

}

const injector = new Injector([
  {provide: UserService, useClass: UserServiceMock},
  {provide: storageFactory, useFactory: storageMockFactory},
]);
const app = injector.get(App);

Differences between di-typescript and angular/di.js

Compared to ts-di and angular/di.js di-typescript uses reflect-metadata to store the meta information. A benefit from using reflect-metadata is, that theInject decorator don't need parameters anymore. di-typescript retrieves these values through the design:paramtypes meta information provided by typescript instead. Another features are the useFactory annotation and the angular2-like {provide: SomeService, useClass/useValue/useFactory} syntax when creating an injector.

Keywords

FAQs

Last updated on 08 Jun 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc