NexusDI
A lightweight and powerful dependency injection framework for Python
NexusDI provides elegant dependency injection with support for multiple lifecycles, automatic dependency resolution, component scanning, and circular dependency handling via lazy proxies.
โจ Key Features
- ๐ Multiple Lifecycles: Singleton, transient, and scoped dependencies
- โก Automatic Resolution: Type-hint based dependency resolution
- โป๏ธ Circular Dependencies: Lazy proxies to handle complex graphs
- ๐ Component Scanning: Auto-discovery of decorated components
- ๐งต Thread-Safe: Reliable for concurrent applications
- ๐ก Lightweight: Minimal overhead with maximum flexibility
๐ Quick Example
from nexusdi import singleton, transient, inject, initialize
@singleton
class DatabaseService:
def __init__(self):
self.connection = "database_connection"
@transient
class UserRepository:
def __init__(self, db_service: DatabaseService):
self.db = db_service
def get_user(self, user_id: int):
return f"User {user_id} from {self.db.connection}"
@inject
def get_user_data(user_repo: UserRepository, user_id: int = 1):
return user_repo.get_user(user_id)
# Initialize the DI system
initialize()
# Dependencies are automatically injected
result = get_user_data(user_id=123)
print(result) # "User 123 from database_connection"
๐ฆ Installation
Install NexusDI with pip:
Or with poetry:
๐ฏ Core Concepts
Lifecycles
singleton: A single instance for the application lifetimetransient: A new instance per requestscoped: Shared within a defined scope
Injection
- Use
@injecton functions to resolve dependencies automatically
Scanning
- Decorate classes to enable automatic component discovery
Circular Dependencies
- NexusDI resolves circular graphs via lazy proxies
๐ Quick Links
- Getting Started - Install and set up NexusDI
- User Guide - Explore lifecycles, injection, and more
- API Reference - Complete API documentation
- Examples - See practical usage examples
๐ค Contributing
Contributions are welcome! Please feel free to open an issue or submit a Pull Request.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ค Author
Harrison Alonso Arroyave Gaviria
- GitHub: @harrison-gaviria
- LinkedIn: Harrison Alonso Arroyave Gaviria
- Email: harrisonarroyaveg@gmail.com