@chubbyjs/chubbyjs-container

A simple PSR-11 container implementation.

Usage no npm install needed!

<script type="module">
  import chubbyjsChubbyjsContainer from 'https://cdn.skypack.dev/@chubbyjs/chubbyjs-container';
</script>

README

chubbyjs-container

CI Build Status Coverage Status Infection MSI

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A minimal Dependency Injection Container (DIC) which implements PSR-11.

Requirements

Installation

Through NPM as @chubbyjs/chubbyjs-container.

npm i @chubbyjs/chubbyjs-container@1.0

Usage

There are two PSR-11 implementations:

  • Container prototype (each get will return a new instance) and shared services
  • MinimalContainer shared services

MinimalContainer / Container

Factories

import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
import PsrContainerInterface from '@chubbyjs/psr-container/dist/ContainerInterface';

const container = new MinimalContainer();

container.factories(
    new Map<string, FactoryInterface>([
        [MyService.name, (container: PsrContainerInterface): MyService => {
            return new MyService(container.get<Logger>(Logger.name));
        }]
    ])
);

Factory

import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
import PsrContainerInterface from '@chubbyjs/psr-container/dist/ContainerInterface';

const container = new MinimalContainer();

// new
container.factory(MyService.name, (container: PsrContainerInterface): MyService => {
    return new MyService(container.get<Logger>(Logger.name));
});

// existing (replace)
container.factory(MyService.name, (container: PsrContainerInterface): MyService => {
    return new MyService(container.get<Logger>(Logger.name));
});

// existing (extend)
container.factory(
    MyService.name,
    (container: PsrContainerInterface, previous?: FactoryInterface): MyService => {
        if (!previous) {
            throw 'Missing service';
        }

        const myService: MyService = previous(container);

        myService.setLogger(container.get<Logger>(Logger.name));

        return myService;
    }
);

Factory with Parameter

import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import Parameter from '@chubbyjs/chubbyjs-container/dist/Parameter';

const container = new MinimalContainer();

container.factory('key', Parameter('value'));

Get

import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';

const container = new MinimalContainer();

container.get<MyService>(MyService.name);

Has

import MinimalContainer from '@chubbyjs/chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';

const container = new MinimalContainer();

container.has(MyService.name);

Container

All methods of the MinimalContainer and the following:

Prototype Factories

each get will return a new instance

import Container from '@chubbyjs/chubbyjs-container/dist/Container';
import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MyService from './Service/MyService';
import PsrContainerInterface from '@chubbyjs/psr-container/dist/ContainerInterface';

const container = new Container();

container.prototypeFactories(
    new Map<string, FactoryInterface>([
        [MyService.name, (container: PsrContainerInterface): MyService => {
            return new MyService(container.get<Logger>(Logger.name));
        }]
    ])
);

Prototype Factory

each get will return a new instance

import Container from '@chubbyjs/chubbyjs-container/dist/Container';
import FactoryInterface from '@chubbyjs/chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MyService from './Service/MyService';
import PsrContainerInterface from '@chubbyjs/psr-container/dist/ContainerInterface';

const container = new Container();

// new
container.prototypeFactory(
    MyService.name,
    (container: PsrContainerInterface): MyService => {
        return new MyService(container.get<Logger>(Logger.name));
    }
);

// existing (replace)
container.prototypeFactory(
    MyService.name,
    (container: PsrContainerInterface): MyService => {
        return new MyService(container.get<Logger>(Logger.name));
    }
);

// existing (extend)
container.prototypeFactory(
    MyService.name,
    (container: PsrContainerInterface, previous?: FactoryInterface): MyService => {
        if (!previous) {
            throw 'Missing service';
        }

        const myService: MyService = previous(container);

        myService.setLogger(container.get<Logger>(Logger.name));

        return myService;
    }
);

Copyright

Dominik Zogg 2021