Code Monkey home page Code Monkey logo

flex-injector's Introduction

Flex-Injector

npm version Downloads

English | 한국어

Flex-Injector is a library that makes dependency injection easy. This library utilizes TypeScript's decorator feature to manage dependencies in a simple and clear way.

Using createInjector, you can create multiple injectors and inject dependencies through different containers. This is particularly useful in a monorepo environment. Each package or module can use an independent injector, making dependency management more efficient and clear.

Installation

  1. Install module:
npm install flex-injector
  1. Install peer dependencies:
npm install reflect-metadata
  1. tsconfig.json compilerOptions :
{
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}

Example of usage

./service
├── todo.service.ts
├── user.service.ts
└── injector.ts
//  ./service/injector.ts

import { createInjector } from 'flex-injector';

const { InjectAble, inject } = createInjector();

export { InjectAble, inject };
//  ./service/todo.service.ts

import { InjectAble } from './injector';

@InjectAble
export class TodoService {

  async getTodo(userId:string) {
    return ...;
  }
}
//  ./service/user.service.ts

import { InjectAble } from './injector';

@InjectAble
export class UserService {

  constructor(private todoService: TodoService) {}


  async getTodo(userId:string) {
    return this.todoService.getTodo(userId);
  }

  async find(userId:string) {
    return ...;
  }
}
// express server example

const userService = inject(UserService);

app.get('/todo', async (req, res) => {
  const todoList = await userService.getTodo(req.session.userId);
  res.json(todoList);
});

❌ Bad Case

//  Beware of circular reference errors. Below is a bad example where circular references occur.

const { inject, InjectAble } = createInjector();

@InjectAble
class A {
  constructor(private b: B) {}
}

@InjectAble
class B {
  constructor(private a: A) {}
}

const a = inject(A); // Throw Circular dependency detected

More examples

flex-injector's People

Contributors

cgoinglove avatar turbobot-temp 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.