All Projects → ceoro9 → ioc-ts

ceoro9 / ioc-ts

Licence: other
Inversion of control container on TypeScript

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ioc-ts

Dry System
Organize your code into reusable components
Stars: ✭ 228 (+1528.57%)
Mutual labels:  ioc, dependency-injection
stashbox
A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
Stars: ✭ 120 (+757.14%)
Mutual labels:  ioc, dependency-injection
Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+19264.29%)
Mutual labels:  ioc, dependency-injection
Php Di
The dependency injection container for humans
Stars: ✭ 2,273 (+16135.71%)
Mutual labels:  ioc, dependency-injection
loopback-next
LoopBack makes it easy to build modern API applications that require complex integrations.
Stars: ✭ 4,412 (+31414.29%)
Mutual labels:  ioc, dependency-injection
Typedi
Simple yet powerful dependency injection tool for JavaScript and TypeScript.
Stars: ✭ 2,832 (+20128.57%)
Mutual labels:  ioc, dependency-injection
hypo-container
A dependency injection container.
Stars: ✭ 16 (+14.29%)
Mutual labels:  ioc, dependency-injection
Qframework
Unity3D System Design Architecture
Stars: ✭ 2,326 (+16514.29%)
Mutual labels:  ioc, dependency-injection
iocgo
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang
Stars: ✭ 36 (+157.14%)
Mutual labels:  ioc, dependency-injection
waiter
Dependency injection, Inversion of control container for rust with compile time binding.
Stars: ✭ 71 (+407.14%)
Mutual labels:  ioc, dependency-injection
ashley
Ashley is a dependency injection container for JavaScript.
Stars: ✭ 23 (+64.29%)
Mutual labels:  ioc, dependency-injection
tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+16685.71%)
Mutual labels:  ioc, dependency-injection
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+1121.43%)
Mutual labels:  ioc, dependency-injection
Testdeck
Object oriented testing
Stars: ✭ 206 (+1371.43%)
Mutual labels:  ioc, dependency-injection
Tsyringe
Lightweight dependency injection container for JavaScript/TypeScript
Stars: ✭ 2,761 (+19621.43%)
Mutual labels:  ioc, dependency-injection
DI-compiler
A Custom Transformer for Typescript that enables compile-time Dependency Injection
Stars: ✭ 62 (+342.86%)
Mutual labels:  ioc, dependency-injection
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (+1042.86%)
Mutual labels:  ioc, dependency-injection
Awilix
Extremely powerful Inversion of Control (IoC) container for Node.JS
Stars: ✭ 2,269 (+16107.14%)
Mutual labels:  ioc, dependency-injection
sector
Simple Injector; Dependency Injection
Stars: ✭ 12 (-14.29%)
Mutual labels:  ioc, dependency-injection
ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (+635.71%)
Mutual labels:  dependency-injection, typescript-library

ioc-ts

Build Status Coverage Status

Inversion of control container on TypeScript

Installing

npm install @ceoro9/ioc-ts reflect-metadata --save

About

ioc-ts is an implementation of inversion on control (IoC) container on TypeScript, that encourages declarational approarch, using decorators and reflection to comfortably and transperrently manage entities and their dependencies.

Usage

This short example just shows the basics, what ioc-ts container is really capable of. Please check out documetation, that will be avaiable soon, to find out more about more sophisticated functionality ioc-ts can offer you.

Step 0: Configure TypeScript compiler

{
  "compilerOptions": {
    ...
    "types": ["reflect-metadata"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Step 1: Imports

import 'reflect-metadata';
import { Injectable, Inject, Container } from '@ceoro9/ioc-ts';

Step 2: Declare your data types and service interfaces

interface IPerson {
  id:        string;
  firstName: string;
  lastName:  string;
}

interface IPersonService {
  createPerson(person: Omit<IPerson, 'id'>): IPerson;
  getPersonById(personId: string): IPerson | undefined;
}

Step 3: Declare your injectable entities, which implement the above service interface

In our example we have two services, which implement IPersonService interface. The first one is PostgreSQL service and second one is MongoDB. Each one corresponds to the database, that it uses under the hood.

const PERSON_POSTGRES_SERVICE_NAME = 'PERSON_POSTGRES_SERVICE';
const PERSON_MONGO_SERVICE_NAME    = 'PERSON_MONGO_SERVICE';

@Injectable(PERSON_POSTGRES_SERVICE_NAME)
class PersonPostgresService implements IPersonService {

  private readonly db: { [id: string]: IPerson | undefined };

  public constructor() {
    this.db = {};
  }

  public createPerson(person: IPerson) {
    const personId = Math.random().toString(36).substring(7);
    this.db[personId] = person;
    return {
      ...person,
      id: personId,
    };
  }

  public getPersonById(personId: string) {
    return this.db[personId];
  }
}

@Injectable(PERSON_MONGO_SERVICE_NAME)
class PersonMongoService implements IPersonService {

  private readonly db: Map<string, IPerson>;

  public constructor() {
    this.db = new Map();
  }

  public createPerson(person: IPerson) {
    const personId = Math.random().toString(36).substring(10);
    this.db.set(personId, person);
    return {
      ...person,
      id: personId,
    };
  }

  public getPersonById(personId: string) {
    return this.db.get(personId);
  }
}

Step 4: Make an injection

And now we have a person controller, which wants to use some service to persist data, but don't want to know implementation details of how this data is persisted and stored. In other words, our person controller should depend upon service interface, but not its concrete implementation. That's why the type of personService property is IPersonService interface. But anyway, we should specify which concrete implementation of service interface personService should take from container. This is where we should make an injection by specifying entity name, which our controller should use. In our case this is property injection by PERSON_POSTGRES_SERVICE_NAME identifier. So in fact, our service will use PostgreSQL service to persist data, but it has no idea about this.

@Injectable()
class PersonController {

  @Inject(PERSON_POSTGRES_SERVICE_NAME)
  private personService: IPersonService;
  
  public get(personId: string) {
    const person = this.personService.getPersonById(personId);
    return (
      person
      ? { status: 200, data: person }
      : { status: 404, data: null }
    );
  }

  public post(personData: Omit<IPerson, 'id'>) {
    const person = this.personService.createPerson(personData);
    return {
      status: 201,
      data: person,
    };
  }
}

Step 5: Obtain reference to global container, where all your injectable entities are stored by default

const container = Container.getGlobal();

Step 6: Get your entities with their resolved dependencies

So you're done. Now you can safely and comfortably work with all of your entities. IoC container will take care of resolving and initializing of their dependencies.

const personController = container.get(PersonController);

const { data: { id: personId } } = personController.post({
  firstName: 'firstName0',
  lastName: 'lastName0',
}); // { status: 201, data: { ... } }
personController.get(personId); // { status: 200, data: { ... } }

License

This project is licensed under the MIT license.

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].