0.1.2 • Published 6 years ago

simple-dependency-container v0.1.2

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

simple-dependency-container

Provides dependency injection for NodeJS apps

No need to use Annotations and Strings to identify dependencies, the actual Class reference is enough.

main.ts

export class Dependency1 {
}

export class Dependency2 {
}

export class Parent1 {
  constructor(
    private dep1: Dependency1,
    private dep2: Dependency2,
    private textValue: string) {
  }
}

container.ts

import { SimpleDependencyContainer } from 'simple-dependency-container';
import { Dependency1, Dependency2, Parent1 } from './main';

let container = new SimpleDependencyContainer();
container.registerType(Dependency1);  // registered as singleton
container.registerType(Dependency2);  // registered as singleton

container.registerType(Parent1)  // registered as singleton
	.argDependency(Dependency1)
	.argDependency(Dependency2)  
	.argValue('Injected Value');

export default container;

server.ts

import container from './container';
import { Dependency1, Dependency2, Parent1 } from './main';

// parent1 will have injected dependencies
let parent1 = container.getInstance(Parent1);

// also you can ask for dependency directly
let dependency1 = container.getInstance(Dependency1);

Note, all instances are singletons.