- Dependencies Class Documentation
The Dependencies
class is a utility designed to manage dependencies in a centralized and controlled manner. It allows you to register, retrieve, and remove dependencies by name, making it ideal for modular applications.
- Centralized Dependency Management: Register and retrieve dependencies globally.
- Type Safety: Supports TypeScript generics for type-safe dependency management.
- Error Handling: Prevents duplicate dependency registration.
Registers a new dependency with a unique name.
- Parameters:
name
(string): The unique name of the dependency.dep
(any): The dependency instance or object to register.
- Throws:
- An error if a dependency with the same name already exists.
Retrieves a registered dependency by its name.
- Parameters:
name
(string): The name of the dependency to retrieve.
- Returns:
- The instance of the dependency if found, or
null
if it does not exist.
- The instance of the dependency if found, or
Removes a registered dependency by its name.
- Parameters:
name
(string): The name of the dependency to remove.
- Returns:
true
if the dependency was successfully removed,false
if it did not exist.
Clears all registered dependencies.
Checks if a dependency with the specified name exists.
- Parameters:
name
(string): The name of the dependency to check.
- Returns:
true
if the dependency exists,false
otherwise.
import { Dependencies } from 's42-core';
// Register a dependency
Dependencies.add('database', { connectionString: 'mongodb://localhost:27017' });
// Check if the dependency exists
if (Dependencies.has('database')) {
console.log('Database dependency exists.');
}
// Retrieve the dependency
const dbConfig = Dependencies.get<{ connectionString: string }>('database');
console.log('Connection String:', dbConfig?.connectionString);
// Remove a specific dependency
Dependencies.remove('database');
// Clear all dependencies
Dependencies.clear();
- Service Management: Store shared services such as database instances, HTTP clients, or configurations.
- Dependency Injection: Simplify dependency injection across different parts of the application.
- Unit Testing: Mock dependencies for isolated testing scenarios.
The add
method will throw an error if you attempt to register a dependency with a name that already exists. Ensure that names are unique within your application:
try {
Dependencies.add('database', {});
Dependencies.add('database', {}); // Throws an error
} catch (error) {
console.error(error.message);
}
This project is licensed under the MIT License. See the LICENSE file for details.