1.2.0 • Published 4 years ago

chubbyjs-container v1.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

chubbyjs-container

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 PSR-11 container implementation.

Requirements

Installation

Through NPM as chubbyjs-container.

npm i 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-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MinimalContainer from 'chubbyjs-container/dist/MinimalContainer';
import MyService from './Service/MyService';
import PsrContainerInterface from 'psr-container/dist/ContainerInterface';

const container = new MinimalContainer();

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

Factory

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

const container = new MinimalContainer();

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

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

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

    const myService: MyService = previous(container);

    myService.setLogger(Logger.name);

    return myService;
});

Factory with Parameter

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

const container = new MinimalContainer();

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

Get

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

const container = new MinimalContainer();

container.get(MyService.name);

Has

import MinimalContainer from '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-container/dist/Container';
import FactoryInterface from 'chubbyjs-container/dist/FactoryInterface';
import Logger from 'some-logger/dist/Logger';
import MyService from './Service/MyService';
import PsrContainerInterface from 'psr-container/dist/ContainerInterface';

const container = new Container();

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

Prototype Factory

each get will return a new instance

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

const container = new Container();

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

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

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

    const myService: MyService = previous(container);

    myService.setLogger(Logger.name);

    return myService;
});

Copyright

Dominik Zogg 2020

1.2.0

4 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago