Giter Site home page Giter Site logo

chubbyts-dic's Introduction

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

chubbyts-dic's People

Contributors

dominikzogg avatar

Watchers

 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.