inversion-of-control
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Install

npm i inversion-of-control

Example

  1. Controller
import { Controller, Injectable } from 'inversion-of-control';
import { UserService } from './services';

@Controller()
@Injectable()
export class UserController {
    constructor(private userService: UserService) {}

    public getUser(): string {
        return this.userService.getUser();
    }
}
  1. Repository
import { Injectable, Repository } from 'inversion-of-control';

@Repository()
@Injectable()
class UserRepository {
    public find(): string {
        return 'User data';
    }
}

export default UserRepository;
  1. Service
import { Injectable, Service } from 'inversion-of-control';
import UserRepository from './repositories';

@Service()
@Injectable()
export class UserService {
    constructor(private userRepository: UserRepository) {}

    public getUser(): string {
        return this.userRepository.find();
    }
}
  1. Setup container and resolve
import DIContainer from 'inversion-of-control';
import { UserController } from './controllers';
import UserRepository from './repositories';
import { UserService } from './services';

const container = new DIContainer();
container.register('UserRepository', UserRepository);
container.register('UserService', UserService);
container.register('UserController', UserController);

const userController = container.resolveController < UserController > 'UserController';
console.log(userController.getUser());

Package Sidebar

Install

npm i inversion-of-control

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

8.15 kB

Total Files

8

Last publish

Collaborators

  • dunghenry