InjectX
A lightweight, easy-to-use service locator pattern implementation for Dart applications. This package provides a simple dependency injection container that helps manage application dependencies with minimal setup.
Features
- Simple registration and retrieval of dependencies
- Singleton instance management
- Automatic disposal of services
- Type-safe dependency injection
- Angular-style inject function
- Zero external dependencies
Installation
Add this to your package's pubspec.yaml file:
dependencies:
inject_x: ^0.0.4
Usage
Basic Usage
// Register your dependencies
InjectX.add<UserService>(UserService());
InjectX.add<AuthService>(AuthService());
// Retrieve instances
final userService = InjectX.get<UserService>();
final authService = InjectX.get<AuthService>();
// Alternative Angular-style injection
final userService = inject<UserService>();
Automatic Disposal
The InjectX automatically handles disposal of services that implement a dispose method:
class DatabaseService {
void dispose() {
// Cleanup resources
}
}
// Register the service
final dbService = InjectX.add<DatabaseService>(DatabaseService());
// Later, when removing the service
InjectX.remove<DatabaseService>(); // dispose() will be called automatically
API Reference
Methods
add<T>(T instance): Register a new dependencyget<T>(): Retrieve a registered dependencyremove<T>(): Remove a registered dependency and dispose if applicablelength: Get the number of registered dependenciesclear: Remove all registered dependencies performing dispose on each one (if supported)
Helper Functions
T inject<T>(): Angular-style dependency injection helper
Error Handling
The package includes proper error handling for common scenarios:
// Attempting to retrieve non-existent dependency
try {
final service = InjectX.get<UnregisteredService>();
} catch (e) {
// Throws StateError: No instance registered for type UnregisteredService
}
Best Practices
- Register dependencies early in your application lifecycle
- Use meaningful type parameters for better code clarity
- Implement dispose methods for services that need cleanup
- Remove services when they're no longer needed
Example
Here's a complete example showing various features:
class UserService {
void dispose() {
// Cleanup
}
}
class AuthService {
final UserService userService;
AuthService(this.userService);
}
void main() {
// Register services
final userService = InjectX.add<UserService>(UserService());
// Create dependent service
final authService = AuthService(inject<UserService>());
InjectX.add<AuthService>(authService);
// Use services
final users = inject<UserService>();
final auth = inject<AuthService>();
// Cleanup
InjectX.remove<AuthService>();
InjectX.remove<UserService>();
}
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.