Giter Site home page Giter Site logo

ts-di's Introduction

⚠️ THIS PACKAGE IS DEPRECATED

Please use https://github.com/mgechev/injection-js instead


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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.