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.

ts-di's People

Contributors

btford avatar buchanae avatar caitp avatar despairblue avatar dmtrs avatar iammerrick avatar joliss avatar kalisjoshua avatar kostyatretyak avatar l8d avatar mikemcelroy avatar pkozlowski-opensource avatar rgbboy avatar robinbuschmann avatar rodyhaddad avatar rwaldron avatar scottasmith avatar shaselton avatar tjclement avatar vojtajina avatar vsavkin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

slavafomin mikalv

ts-di's Issues

Not working

Hello!

I've installed the module and tried to use example from the README, but it's not working.

userService is not injected to the App constructor.

Is this module works? Are you using it in production?

error TS7006: Parameter '_meta' implicitly has an 'any' type.

Hello!

Thank you for this great library!

However, when I try to compile it with a simple app, I'm getting the following error:

$ tsc

220         const foundMeta = meta.find(_meta => _meta.index === index);
                                        ~~~~~

node_modules/di-typescript/lib/annotations.ts(220,37): error TS7006: Parameter '_meta' implicitly has an 'any' type.
$ tsc --version
Version 2.2.1

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES6",
    "alwaysStrict": true,
    "listEmittedFiles": true,
    "noImplicitAny": true,
    "outDir": "dist/",
    "pretty": true,
    "removeComments": true,
    "sourceMap": true,
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules/"
  ]
}

What could be the problem?

Thanks!

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.