@soerenuhrbach/di
v1.2.3
Published
A lightweight dependency injection container for TypeScript/JavaScript for constructor injection. Based on [tsyringe](https://github.com/microsoft/tsyringe).
Downloads
8
Readme
@soerenuhrbach/di
A lightweight dependency injection container for TypeScript/JavaScript for constructor injection. Based on tsyringe.
Installation
Install by npm
npm install --save @soerenuhrbach/di
or install with yarn
yarn add @soerenuhrbach/di
Modify your tsconfig.json
to include the following settings
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
Add a polyfill for the Reflect API. You can use:
The Reflect polyfill import should only be added once, and before before DI is used:
// main.ts
import "reflect-metadata";
// Your code here...
API
The container performs Constructor Injection on the constructors of decorated classes.
Decorators
@Injectable()
Class decorator factory that allows the class' dependencies to be injected at runtime.
Usage
import { Injectable } from "@soerenuhrbach/di";
@Injectable()
class Foo {
constructor(private database: Database) {}
}
// some other file
import "reflect-metadata";
import { container } from "@soerenuhrbach/di";
import { Foo } from "./foo";
const instance = container.resolve(Foo);
@Singleton()
Class decorator factory that registers the class as a singleton within the global container.
Usage
import { Singleton } from "@soerenuhrbach/di";
@Singleton()
class Foo {
constructor() {}
}
// some other file
import "reflect-metadata";
import { container } from "@soerenuhrbach/di";
import { Foo } from "./foo";
const instance = container.resolve(Foo);
@AutoInjectable()
Class decorator factory that replaces the decorated class' constructor with a parameterless constructor that has dependencies auto-resolved.
Usage
import { AutoInjectable } from "@soerenuhrbach/di";
@AutoInjectable()
class Foo {
constructor(private database?: Database) {}
}
// some other file
import { Foo } from "./foo";
const instance = new Foo();
Notice how in order to allow the use of the empty constructor new Foo()
, we
need to make the parameters optional, e.g. database?: Database
.
@Inject()
Parameter decorator factory that allows for interface and other non-class information to be stored in the constructor's metadata.
Usage
import { Injectable, Inject } from "@soerenuhrbach/di";
interface Database {
// ...
}
@Injectable()
class Foo {
constructor(@Inject("Database") private database?: Database) {}
}
@InjectAll()
Parameter decorator for array parameters where the array contents will come from the container. It will inject an array using the specified injection token to resolve the values.
Usage
import { Injectable, InjectAll } from "@soerenuhrbach/di";
@Injectable()
class Foo {}
@Injectable()
class Bar {
constructor(@InjectAll(Foo) fooArray: Foo[]) {
// ...
}
}
Providers
A provider is registered with the DI container and provides the container the information needed to resolve an instance for a given token.
ClassProvider
This provider is used to resolve classes by their constructor. When registering a class provider you can simply use the constructor itself, unless of course you're making an alias (a class provider where the token isn't the class itself).
Usage
import { container, ClassProvider } from '@soerenuhrbach/di';
class Foo {}
const provider = new ClassProvider(Foo);
container.register("Foo", provider);
ValueProvider
This provider is used to resolve a token to a given value. This is useful for registering constants, or things that have a already been instantiated in a particular way.
Usage
import { container, ValueProvider } from '@soerenuhrbach/di';
const provider = new ValueProvider("Bar");
container.register("Foo", provider);
TokenProvider
This provider can be thought of as a redirect or an alias, it simply states that given token x, resolve using token y.
Usage
import { container, TokenProvider } from '@soerenuhrbach/di';
class Foo {}
container.register("Foo", Foo);
const provider = new TokenProvider("Foo");
container.register("FooBar", provider);
FactoryProvider
This provider is used to resolve a token using a given factory. The factory has full access to the dependency container.
Usage
import { container, FactoryProvider, ContainerInterface } from '@soerenuhrbach/di';
class Foo { constructor(public bar: string) {} }
const FooFactory = (container: ContainerInterface) => new Foo("Bar");
const provider = new FactoryProvider(FooFactory);
container.register("Foo", provider);