Skip to content

NexusDI

PyPI version Python versions PyPI - Status License: MIT

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:

pip install nexusdi

Or with poetry:

poetry add nexusdi

๐ŸŽฏ Core Concepts

Lifecycles

  • singleton: A single instance for the application lifetime
  • transient: A new instance per request
  • scoped: Shared within a defined scope

Injection

  • Use @inject on functions to resolve dependencies automatically

Scanning

  • Decorate classes to enable automatic component discovery

Circular Dependencies

  • NexusDI resolves circular graphs via lazy proxies


๐Ÿค 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