@jdl2/ioc

2.0.0-alpha.30 • Public • Published

ioc

This is ioc documentation

If you want to have control over how instances are created, it's possible to implement and register a provider. For example. to automatically create logger instances with the name of a class when @inject Logger log, you would do something like this:

import { createLogger, Logger } from '@jdl2/log';
import { KeyMap } from '@jdl2/log';
import { Injector } from '@jdl2/ioc';

export function configure(injector: Injector) {
    const loggerInstances = new KeyMap();
    function LoggerProvider(i: Injector, obj: object): Logger {
        return loggerInstances.getOrCreate(obj.constructor, () => createLogger(obj.constructor.name));
    }
    injector.bind(Logger).toProvider(LoggerProvider);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @jdl2/ioc

Weekly Downloads

0

Version

2.0.0-alpha.30

License

none

Unpacked Size

1.3 kB

Total Files

2

Last publish

Collaborators

  • kzantow