Skip to content

Dependency injection container (DIC), PSR-11 inspired.

License

Notifications You must be signed in to change notification settings

chubbyts/chubbyts-dic

Repository files navigation

chubbyts-dic

CI Coverage Status Mutation testing badge npm-version

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

Dependency injection container (DIC), PSR-11 inspired.

Requirements

Installation

Through NPM as @chubbyts/chubbyts-dic.

npm i @chubbyts/chubbyts-dic@^1.2.0

Usage

Sets

import { Container } from '@chubbyts/chubbyts-dic-types/dist/container';
import { createContainer } from '@chubbyts/chubbyts-dic/dist/container';
import { Logger } from 'some-logger/dist/logger';
import { createMyService, decorateMyService, MyService } from './service/my-service';

const container = createContainer();

container.sets(new Map([
    ['myService', (container: Container): MyService => {
        return createMyService(container.get<Logger>('logger'));
    }]
]));

Set

import { Container } from '@chubbyts/chubbyts-dic-types/dist/container';
import { createContainer, createParameter, Factory } from '@chubbyts/chubbyts-dic/dist/container';
import { Cache } from 'some-cache/dist/cache';
import { Logger } from 'some-logger/dist/logger';
import { createMyService, MyService } from './service/my-service';

const container = createContainer();

// new
container.set('myService', (container: Container): MyService => {
    return createMyService(container.get<Logger>('logger'));
});

// existing (replace)
container.set('myService', (container: Container): MyService => {
    return createMyService(container.get<Logger>('logger'));
});

// existing (extend)
container.set('myService', (container: Container, existingFactory?: Factory): MyService => {
    if (!existingFactory) {
        throw 'Missing service';
    }

    return decorateMyService(existingFactory(container), container.get<Cache>('cache'));
});

// parameter
container.set('param', createParameter('value'));

Get

import { createContainer } from '@chubbyts/chubbyts-dic/dist/container';
import { MyService } from './service/my-service';

const container = createContainer();

container.get<MyService>('myService');

Has

import { createContainer } from '@chubbyts/chubbyts-dic/dist/container';

const container = createContainer();

container.has('myService');

Copyright

2024 Dominik Zogg

About

Dependency injection container (DIC), PSR-11 inspired.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published