0.5.0 • Published 5 years ago

iocfy-ts v0.5.0

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

Simple IoC implementation of TypeScript.

Installation

$ npm i -S iocfy-ts

Getting Started

import iocfy from 'iocfy-ts';
import { Bean, Inject } from 'iocfy-ts';

// by default, the bean's name is the class name. 
@Bean()
class UserDao {
  findUser() {
    return { name: 'Justin', sex: 'male', };
  }
}

// or you can specify the bean name.
@Bean('userService')
class UserService {
  name: string;
  
  @Inject('UserDao')
  userDao: UserDao;

  printUser() {
    const user = this.userDao.findUser();
    console.log(user);
  }
  
  findUser(id: number) {
    return this.userDao.findUser();
  }
}

// you can assignment the normal properties.
@Bean('UserController', { limit: 20 })
class UserController {
  limit: number;
  
  @Inject('userService')
  userService: UserService;
  
  queryById() {
    return this.userService.findUser(1);
  }
}

iocfy.init();

const userService = <UserService>iocfy.get('UserService');
userService.printUser();

compile and run.

If you want to see the debug info, set environment DEBUG to iocfy, eg:

$ export DEBUG=iocfy
$ node index.js

or

$ DEBUG=iocfy node index.js

FAQ

I got a undefined value When I use iocfy.get.

Make sure the files which have @Bean are loaded. You can print debug info to see it. If the beans were loaded, the debug info would like this:

$ iocfy Init bean: UserDao. +0ms
$ iocfy Init bean: UserService. +0ms
0.5.0

5 years ago

0.4.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago