Skip to main content

.NET Core Dependency Injection for Python

Project description

WD-DI

.NET Style Dependency Injection for Python

PyPI Version Python Version CI Status License Built by white duck LinkedIn Bluesky

WD-DI brings the robust and flexible dependency injection patterns of .NET to your Python applications, with no external library dependencies, just Python's standard library.

Full documentation can be found at https://whiteducksoftware.github.io/wd-di/ (or in the /docs directory).

For beginners, the Core Concepts and Tutorials sections in the documentation are a great place to start to understand not just how to use DI, but why it's beneficial!


Why WD-DI? 🤔

  • Simplified Dependency Management: Effortlessly manage object creation and lifecycles.
  • Enhanced Testability: Easily mock dependencies for robust unit tests.
  • Modular Architecture: Build loosely coupled, maintainable, and scalable applications.
  • Familiar Patterns: Leverage .NET-inspired DI concepts like service lifetimes (Singleton, Scoped, Transient), constructor injection, and the Options pattern for configuration.
  • Pythonic and Lightweight: Clean, intuitive API that integrates smoothly into your Python projects.
✅ Feature WD-DI Typical alternatives
Pure-stdlib (zero runtime deps) ✘ pull in pydantic / C-extensions
.NET-style lifetimes
Transient • Singleton • Scoped (+ auto-dispose)
Scoped rarely supported
Strongly-typed Options binding from dict/JSON/env → dataclass Dicts or manual parsing
Middleware pipeline bundled & DI-aware (async-first) None ship a generic pipeline
Decorator-based registration tied to your ServiceCollection (no globals) Global singletons or meta-classes
Thread-safe circular-dependency detection via ContextVar Simple set → race-prone
Full IDE type inference for get_service() Returns Any / needs cast()
Lean & readable (~1.4 k LOC + robust test suite) wtf am I reading

Installation 📦

pip install wd-di

Quick Example: The Power of WD-DI 🚀

Experience clean, decoupled code with intuitive type-hinted dependency resolution:

from wd.di import ServiceCollection

services = ServiceCollection()

# Define interfaces and implementations
class IEmailService:
    def send_email(self, to: str, subject: str, body: str) -> None:
        pass

class EmailService(IEmailService):
    def send_email(self, to: str, subject: str, body: str) -> None:
        print(f"\nSending email to {to}")
        print(f"Subject: {subject}")
        print(f"Body: {body}")
        print("Email sent successfully\n")

# UserService depends on IEmailService
class UserService:
    def __init__(self, email_service: IEmailService):
        self.email_service = email_service

    def notify_user(self, user_id: str, message: str) -> None:
        self.email_service.send_email(f"user{user_id}@example.com", "Notification", message)

# Register services
services.add_singleton(IEmailService, EmailService)
services.add_singleton(UserService)

# Build provider
# Services will be resolved and injected into services 
# like EmailService into UserService
provider = services.build_service_provider()

# Get services with proper type hints
email_service = provider.get_service(IEmailService)
user_service = provider.get_service(UserService)

# IDE will provide code completion for these methods
# No need to cast or use Any
email_service.send_email("test@example.com", "Test", "Hello")
user_service.notify_user("123", "Welcome!")

Dive into the full documentation to explore service lifetimes, configuration, middleware, and more!


Contributing 🤝

Contributions are welcome! Please see the main documentation site for details on how to contribute, report issues, or request features.


License 📜

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wd_di-0.2.7.tar.gz (93.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wd_di-0.2.7-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file wd_di-0.2.7.tar.gz.

File metadata

  • Download URL: wd_di-0.2.7.tar.gz
  • Upload date:
  • Size: 93.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for wd_di-0.2.7.tar.gz
Algorithm Hash digest
SHA256 5a16a6d22c2dbb1ada19d184660abdc6366979ecfdc95ed476d922f0b1a68ef0
MD5 0afda5ce221c3d8aa90e55f6071d4c66
BLAKE2b-256 8620358d340b00a7271b87182d3418ce354e793f73916d71bbd461e3e44caf5f

See more details on using hashes here.

File details

Details for the file wd_di-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: wd_di-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for wd_di-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 6183dcb0222ce6ef1f7f614328784b758556c9d53728a49a027fc4de2f11bd09
MD5 e7d63480c3c633561c967aa31f090cbd
BLAKE2b-256 8c4c811df8d869d8bf5828a393fcd36d2729527f2c215bb2a268d87feea5c0f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page