A simple and powerful dependency injection, service container, instance resolver for Node applications
The Service container is a powerful tool for managing class dependencies and performing dependency injection.
For example, suppose you have a simple UserService
class that contains all users
from database:
// written in Javascript
class UserService {
@autoload('UserRepository') userRepository
getUsers() {
return this.userRepository.getUsers()
}
}
In this example, the UserService needs to retrieve users from a data source. So, we will inject a repository that is able to retrieve users. Since the repository is injected, we are able to easily swap it out with another implementation. We are also able to easily "mock", or create a dummy implementation of the UserRepository when testing our application.
MIT @ Nhat Phan