dinkyinject
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

DinkyInject

A simple and lightweight Dependency Injector

Installation

npm i dinkyinject

Usage

import { Inject, InjectorFactory } from 'dinkyinject';

// first service
interface IUserRepository {
    loadUser(): any[];
}

class MyUserRepository implements IUserRepository {
    loadUser(): any[] {
        return ['Peter', 'Lois', 'Brian', 'Stewie']
    }
}

// second service
interface IMyService {
    readonly repository: IUserRepository;
}

class MyService implements IMyService {
    @Inject('userRepository')
    public repository: IUserRepository = null;
}

// create injector factory
const factory = new InjectorFactory();

// register services
factory.registerService('userRepository', () => new MyUserRepository())
factory.registerService(MyService, new MyService())

// create injector
const injector = factory.createInjector();

// resolve autowired service
const service = injector.get<IMyService>(MyService);

// 'Peter', 'Lois', 'Brian', 'Stewie'
console.log(service.repository.loadUser());

Readme

Keywords

none

Package Sidebar

Install

npm i dinkyinject

Weekly Downloads

20

Version

1.1.0

License

Apache-2.0

Unpacked Size

21.4 kB

Total Files

7

Last publish

Collaborators

  • tomamatics