A lightweight and flexible dependency injection container for Node.js, designed to simplify the management of service dependencies in your applications. The ioc
package provides a straightforward approach to implementing inversion of control, supporting singleton and transient service lifetimes, and facilitating testing with mock implementations.
- Features ✨
- Installation 📦
- Quick Start 🚀
- Documentation 📚
- Contributing 🤝
- Issues and Feature Requests 🔍
- License ⚖️
- Simple API: Intuitive methods for registering, resolving, and mocking dependencies.
- Support for Mocks: Easily replace real implementations with mocks for testing.
- Singletons and Transient Services: Choose between single instance (singleton) or multiple instances (transient) for your services.
- TypeScript Support: Fully typed for excellent IntelliSense and type checking.
npm install @hygorchristian/ioc
# or
yarn add @hygorchristian/ioc
Import InversionOfControl
from the package and instantiate it:
import InversionOfControl from '@hygorchristian/ioc';
// Define your container contract
interface Dependencies {
'Core/Env': Env;
'Service/Database': Database;
}
const ioc = new InversionOfControl<Dependencies>();
Note: The
Dependencies
interface is used to define the contract of your container. It should contain the keys of your services and their respective types. This is optional, but recommended for better type checking and IntelliSense.
Register your services with the container:
// Register a transient service
ioc.register('Core/Env', () => new Env());
// Register a service that depends on another service
ioc.register('Service/Database', dependencies => {
const env = dependencies.use('Core/Env');
return new Config(env);
});
// Register a singleton service that depends on another service
ioc.registerSingleton('Service/Database', dependencies => {
const config = dependencies.use('Service/Config');
return new Database(config);
});
Note: It's recommended to register your services in the entry point of your application, such as
index.ts
orapp.ts
if you want your dependencies to be globally available.
Resolve an instance of your service:
const database = ioc.use('Service/Database');
Replace implementations with mocks for testing purposes:
ioc.mock('Service/Database', () => new MockDatabase());
ioc.enableMocks();
const mockDatabase = ioc.use('Service/Database');
For detailed documentation, including advanced features and examples, visit GitHub repository documentation.
Contributions are welcome! Please refer to the contribution guidelines for more information.
Encountered a bug or have an idea? Please open an issue on the GitHub Issue Tracker.
ioc
is licensed under the MIT License. See the LICENSE file for more details.