Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "tsyringe", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Lightweight dependency injection container for JavaScript/TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,4 +5,93 @@ [![Travis](https://img.shields.io/travis/Microsoft/tsyringe.svg)](https://travis-ci.org/Microsoft/tsyringe/) | ||
A lightweight dependency injection container for TypeScript/TypeScript | ||
A lightweight dependency injection container for TypeScript/TypeScript for | ||
constructor injection. | ||
## Installation | ||
```sh | ||
npm install --save tsyringe | ||
``` | ||
Modify your `tsconfig.json` to include the following settings | ||
```json | ||
{ | ||
"compilerOptions": { | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true | ||
} | ||
} | ||
``` | ||
## Usage | ||
### Example without interfaces | ||
Since classes have type information at runtime, we can resolve them without any | ||
extra information. If a particular class isn't registered with the container then | ||
a new instance will be resolved each time. | ||
```TypeScript | ||
// Foo.ts | ||
export class Foo {} | ||
``` | ||
```TypeScript | ||
// Bar.ts | ||
import {Foo} from "./Foo"; | ||
import {decorators} from "tsyringe"; | ||
const {injectable} = decorators; | ||
@injectable() | ||
export class Bar { | ||
constructor(public myFoo: Foo) {} | ||
} | ||
``` | ||
```TypeScript | ||
// main.ts | ||
import {container} from "tsyringe"; | ||
import {Bar} from "./Bar"; | ||
const myBar = container.resolve(Bar); | ||
// myBar.myFoo => An instance of Foo | ||
``` | ||
### Example with interfaces | ||
Interfaces don't have type information at runtime, so we need to decorate them | ||
with `@inject(...)` so the container knows how to resolve them. | ||
```TypeScript | ||
// SuperService.ts | ||
export interface SuperService { | ||
// ... | ||
} | ||
``` | ||
```TypeScript | ||
// TestService.ts | ||
import {SuperService} from "./SuperService"; | ||
export class TestService implements SuperService { | ||
//... | ||
} | ||
``` | ||
```TypeScript | ||
// Client.ts | ||
import {decorators} from "tsyringe"; | ||
const {injectable, inject} = decorators; | ||
@injectable() | ||
export class Client { | ||
constructor(@inject("SuperService") private service: SuperService) {} | ||
} | ||
``` | ||
```TypeScript | ||
// main.ts | ||
import {Client} from "./Client"; | ||
import {TestService} from "./TestService"; | ||
import {container} from "tsyringe"; | ||
container.register({ | ||
token: "SuperService", | ||
useClass: TestService | ||
}); | ||
const client = container.resolve(Client); | ||
// client's dependencies will have been resolved | ||
``` | ||
## Contributing | ||
@@ -9,0 +98,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16129
109