This repository contains a project with dependency injection pattern.
Para utilizá-lo você necessitará da versão mais recente do Node.js e do npm.
git clone https://github.com/MurylloEx/typescript-dependency-injection-pattern.git
npm install
Declaring services
@Service()
class DatabaseService {
constructor() {}
insert(table: string, data: { [column: string]: string | number }) {
console.log(`writing to ${table}:`);
console.log(data);
}
}
@Service()
class LoggerService {
constructor(private dbService: DatabaseService) {}
info(message: string) {
this.dbService.insert('log', {
level: 200,
message: message
});
}
}
@Service()
class UserService {
constructor(private logger: LoggerService) {}
editUser(userId: number) {
this.logger.info(`User ${userId} has been edited`);
}
}
Loading and using services
const container = ServiceContainer.create().providers([
LoggerService,
UserService,
DatabaseService
]);
Injector.create(container).resolveAll();
const userService: UserService = container.get<UserService>(UserService);
userService.editUser(3);
Muryllo Pimenta – muryllo.pimenta@upe.br
Distribuído sobre a licença MIT. Veja LICENSE
para mais informações.
- Fork it (https://github.com/MurylloEx/typescript-dependency-injection-pattern/fork)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request