Code Monkey home page Code Monkey logo

chubbyjs-container's Introduction

chubbyjs-container

CI 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/[email protected]

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

chubbyjs-container's People

Contributors

dominikzogg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.