Node JS
  • SetUp Node&NVM
  • Folder Structure
  • Setup NodeJS Project
  • Library
    • Awilix
    • Swagger
    • Express
    • Jest
      • Supertest
    • Prettier
    • Eslint & Tslint
      • Tslint & Prettier
      • Tslint Plugin Prettier
      • Config With Airbnb
    • Husky And Friends
    • Sentry
  • INFRASTRUCTURE
    • Docker
      • Docker image
      • Docker container
      • Docker Volume
      • Docker command
      • Docker Compose
      • Problem & Solution
    • SonarQube
      • How to use in Nodejs
    • NGinX
    • ดูเพิ่มเติม
  • Note
    • .env declare type
    • Learn Link
Powered by GitBook
On this page
  • ความหมาย
  • install
  • Usage
  1. Library

Awilix

Dependency Injection (DI) container สำหรับ JavaScript/Node

ความหมาย

  • Dependent แปลว่า

    • (adj) ขึ้นอยู่กับ, ต้องอาศัย, ต้องพึ่งพา

    • (n) คนที่คอยพึ่งผู้อื่น, ผู้อาศัย, คนรับใช้

  • Dependency หมายถึงสิ่งที่ขึ้นอยู่กับสิ่งอื่น โดยในทาง programming แล้ว object ที่เป็น dependency นั้น จะขึ้นอยู่กับ class อื่นๆ ซึ่งไม่ใช่ class ที่เราสนใจอยู่

  • Injection นั้นเป็น noun ของคำว่า inject ซึ่งแปลได้ว่า ฉีด(ยา) หรืออีกความหมายหนึ่งก็คือการนำบางอย่างเข้าสู่บางสิ่ง ซึ่งแปลง่ายๆได้ว่า การส่งต่อนั้นเอง

  • Dependency Injection ก็คือเทคนิคหนึ่งในการเขียนโปรแกรม โดยจะใช้การส่งต่อ (inject) ตัว dependency แทนการสร้าง dependency ขึ้นมาใหม่

ประโยชน์ของ Dependency Injection

  • ลดการ couple(ผูกมัด) ของ code

    ถ้าเราใช้เทคนิค dependency injection แล้ว code ในส่วนของ dependency ก็จะไม่ผูกหรือขึ้นอยู่กับ class ของเรา ทำให้นำไปสู่ข้อต่อไป

  • ง่ายต่อการ test

    เมื่อ code ผูกมัดกัน(couple)น้อยลงแล้ว ทำให้เวลาเรา test สามารถ mock(จำลอง) ส่วนของ dependency ได้ จึงทำให้ง่ายต่อการเขียน test ครับ

install

npm install awilix

Usage

  • Create a container

  • Register some modules in it

  • Resolve and use!

#user.interface.js
export interface IUserController
{
  getUser(xtc)
}

#user.controller.js
export class UserController implement IUserController{
  // We are using constructor injection.
  constructor(opts:unknow) {
    // Save a reference to our dependency.
    this.userService = opts.userService
  }

  // imagine ctx is our HTTP request context...
  getUser(ctx) {
    return this.userService.getUser(ctx.params.id)
  }
}

#user.service.js
export const makeUserService = ({ db }) => {
  return {
    getUser: id => {
      return db.query(`select * from users where id=${id}`)
    }
  }
}

#index.js
import { asClass, asFunction, createContainer, InjectionMode } from 'awilix';
const awilixcontainer = createContainer<registerClassType>({
    injectionMode: InjectionMode.PROXY,
});
awilixcontainer.createScope();
container.register({
  userController: asClass(UserController)
  userService: asFunction(makeUserService)
})

const router = express.Router();
router.get('/api/users/:id', awilixcontainer.resolve('userController').getUser)

การเรียกใช้ awilixcontainer.resolve('userController') ซึ่งจะ return เป็น instant ของคลาส UserController

ตัวแปร contructor จะส่งไปในรูปแบบของ object ซึ่งจะถูกบรรจะด้วย class ที่เรา register เข้าไป

PreviousSetup NodeJS ProjectNextSwagger

Last updated 3 years ago

npm: awilixnpm
อ่านเพิ่มเติม
Logo