Dioxide offers a unique blend of performance and simplicity for Python dependency injection. With its declarative API, type safety, and Rust-backed graph resolution, it enhances testability and loose coupling in applications. Ideal for developers seeking a robust framework without sacrificing efficiency.
dioxide is a fast and efficient dependency injection framework for Python that integrates Rust's performance and a declarative approach for defining dependencies. This framework enables developers to write cleaner and more maintainable code while adhering to the Dependency Inversion Principle. The key features include:
Dioxide employs a hexagonal architecture to facilitate clean and testable code through a straightforward implementation process. A typical usage example is:
from typing import Protocol
from dioxide import Container, Profile, adapter, service
# Define a protocol for email sending
class EmailPort(Protocol):
async def send(self, to: str, subject: str, body: str) -> None: ...
# Create an adapter for production environment
@adapter.for_(EmailPort, profile=Profile.PRODUCTION)
class SendGridAdapter:
async def send(self, to: str, subject: str, body: str) -> None:
print(f"📧 Sending via SendGrid to {to}: {subject}")
# Service that uses the EmailPort
@service
class UserService:
def __init__(self, email: EmailPort):
self.email = email
async def register_user(self, email_addr: str, name: str):
await self.email.send(
to=email_addr,
subject="Welcome!",
body=f"Hello {name}, thanks for signing up!"
)
# Using the container to resolve dependencies
container = Container()
container.scan(profile=Profile.PRODUCTION)
user_service = container.resolve(UserService)
await user_service.register_user("user@example.com", "Alice")
Dioxide supports multiple platforms and Python versions across:
Dioxide also provides lifecycle management capabilities for components that require initialization and cleanup. The @lifecycle decorator allows efficient management of component lifetimes, ensuring a structured approach to setting up and tearing down resources.
The framework supports function injection for flexibility, enabling dependency injection into standalone functions, making it ideal for web frameworks like FastAPI and background job systems.
dioxide is marked as stable, with the current API frozen to prevent breaking changes until v2.0. Future features will enhance the framework's capabilities, including request scoping and property injection.
For detailed insights into the architecture and other advanced usage patterns, please refer to the comprehensive documentation available within the repository.
No comments yet.
Sign in to be the first to comment.