All Projects → typesoft → Container Ioc

typesoft / Container Ioc

Licence: mit
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.

Programming Languages

typescript
32286 projects
es6
455 projects
es2015
71 projects
es2017
19 projects
es2016
15 projects

Projects that are alternatives of or similar to Container Ioc

CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (-66.29%)
Mutual labels:  ioc, container, injection, inversion-of-control, di, dependency
Tsyringe
Lightweight dependency injection container for JavaScript/TypeScript
Stars: ✭ 2,761 (+3002.25%)
Mutual labels:  dependency, dependency-injection, ioc, injection, container
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-75.28%)
Mutual labels:  ioc, dependency-injection, injection, inversion-of-control, di
waiter
Dependency injection, Inversion of control container for rust with compile time binding.
Stars: ✭ 71 (-20.22%)
Mutual labels:  ioc, dependency-injection, container, inversion-of-control, di
Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+195.51%)
Mutual labels:  ioc, dependency-injection, container, injection, di
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+49.44%)
Mutual labels:  dependency-injection, ioc, injection, inversion-of-control
Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+2946.07%)
Mutual labels:  dependency-injection, ioc, inversion-of-control, di
Zenject-2019
Dependency Injection Framework for Unity3D
Stars: ✭ 2,567 (+2784.27%)
Mutual labels:  ioc, dependency-injection, injection, dependency
Hiboot
hiboot is a high performance web and cli application framework with dependency injection support
Stars: ✭ 150 (+68.54%)
Mutual labels:  dependency-injection, ioc, di, container
Ditranquillity
Dependency injection for iOS (Swift)
Stars: ✭ 317 (+256.18%)
Mutual labels:  dependency-injection, ioc, inversion-of-control, di
Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (+233.71%)
Mutual labels:  dependency-injection, ioc, injection, inversion-of-control
diod
A very opinionated inversion of control (IoC) container and dependency injector for Typescript, Node.js or browser apps.
Stars: ✭ 36 (-59.55%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, di
stashbox
A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
Stars: ✭ 120 (+34.83%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, di
typeioc
Dependency injection container for typescript / javascript
Stars: ✭ 32 (-64.04%)
Mutual labels:  ioc, container, injection, dependency
Singularity
A extremely fast ioc container for high performance applications
Stars: ✭ 63 (-29.21%)
Mutual labels:  dependency-injection, ioc, inversion-of-control, di
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (-66.29%)
Mutual labels:  ioc, dependency-injection, injection, dependency
fusion
A simple automated dependency injection library for TypeScript, supporting React class and functional components.
Stars: ✭ 18 (-79.78%)
Mutual labels:  ioc, dependency-injection, injection
siringa
Minimalist dependency injection library for Python that embraces type annotations syntax
Stars: ✭ 51 (-42.7%)
Mutual labels:  dependency-injection, inversion-of-control, dependency
Kodein Mvvm
Example app using Kodein for dependency injection with MVVM and Architecture Components
Stars: ✭ 26 (-70.79%)
Mutual labels:  dependency, dependency-manager, dependency-injection
granitic
Web/micro-services and IoC framework for Golang developers
Stars: ✭ 32 (-64.04%)
Mutual labels:  ioc, dependency-injection, inversion-of-control

alt text

container-ioc

is a Dependency Injection / Inversion of Control (IoC) container package for Javascript and Node.js applications powered by Typescript . It manages the dependencies between classes, so that applications stay easy to change and maintain as they grow.

npm version Build Status npm Gitter chat license

Features:

Examples:

Installation:

npm install --save container-ioc

Basics:

Code examples below are written in Typescript. Check examples/javascript for examples written in Javascript.

Step 1. Define your interfaces and types.

Possible values for types: Symbol, string, Object.

interface IApplication {
    run(): void;
}

interface IService {
    serve(): void;
}

const TApplication = Symbol('IApplication');

const TService = Symbol('IService');

Step 2. Declare dependencies with decorators Injectable and Inject.

import { Injectable, Inject } from 'container-ioc';

@Injectable()
export class Application implements IApplication {
    constructor(@Inject(TService) private service: IService) {}
    
    run(): void {
        this.service.serve();
    }
}

@Injectable()
export class Service implements IService {
    serve(): void {
        // serves
    }
}

Step 3. Create a container and register types in there.

import { Container } from 'container-ioc';

let container = new Container();

container.register([
    { token: TApplication, useClass: Application },
    { token: TService, useClass: Service }
]);

Step 4. Resolve value from the container.

let app = container.resolve(TApplication);

app.run();

Step 2 for Javascript.

Since Javascript does not support parameter decorators, use alternative API for declaring dependencies. In this case we don't use Inject decorator. See examples/javascript for more.

@Injectable([TService])
class Service {
    constructor(service) {
        this.service = service;
    }
}

Life Time control

By default, containers resolve singletons when using useClass and useFactory. Default life time for all items in a container can be set by passing an option object to it's contructor with defailtLifeTime attribute. Possible values: LifeTime.PerRequest (resolves instances) and LifeTime.Persistent (resolves singletons);

import { LifeTime } from 'container-ioc';

const container = new Container({
    defaultLifeTime: LifeTime.PerRequest
});

You can also specify life time individually for each item in a container by specifying lifeTime attribute.

container.register([
    {
        token: TService,
        useClass: Service,
        lifeTime: LifeTime.PerRequest
    }
]);
container.register([
    {
        token: TService,
        useFactory: () => {
            return {
                serve(): void {}
            }
        },
        lifeTime: LifeTime.Persistent
    }
]);

Hierarchical containers

If a container can't find a value within itself, it will look it up in ascendant containers. There a 3 ways to set a parent for a container.

1. Container.createChild() method.
const parentContainer = new Container();
const childContainer = parentContainer.createChild();
2. Container.setParent() method.
const parent = new Container();
const child = new Container();

child.setParent(parent);
3. Via Container's constructor with options.
const parent = new Container();
const child = new Container({
    parent: parent
});

Using Factories

/* Without injections */
container.register([
    {
        token: 'TokenForFactory',
        useFactory: () => {
            return 'any-value';
        }
    }
]);

/* With injections */
container.register([
    { token: 'EnvProvider', useClass: EnvProvider },
    {
        token: 'TokenForFactory',
        useFactory: (envProvider) => {
            // do something
            return 'something';
        },
        inject: ['EnvProvider']
    }
]);

Using Values

container.register([
    { token: 'IConfig', useValue: {}}
]);

Shortcut for Classes

container.register([
    App
]);

Is the same as:

container.register([
    { token: App, useClass: App }
]);

Contribution

Become a contributor to this project. Feel free to submit an issue or a pull request.

see CONTRIBUTION.md for more information.

Please see also our Code of Conduct.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].