Skip to main content

.NET Core Dependency Injection for Python

Project description

WD-DI: .NET Style Dependency Injection for Python 🐍

PyPI version Build Status

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. 🐍

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

# 1. Create a service collection
services = ServiceCollection()

# 2. Define your services (interfaces optional but recommended)
class IEmailService:
    def send(self, message: str): ...

@services.singleton(IEmailService) # Register EmailService as a singleton for IEmailService
class EmailService(IEmailService):
    def send(self, message: str):
        print(f"Sending email: {message}")

@services.transient() # Register NotifierService as transient (new instance each time)
class NotifierService:
    def __init__(self, emailer: IEmailService): # Dependency injected here!
        self._emailer = emailer

    def notify_admin(self, alert: str):
        self._emailer.send(f"Admin Alert: {alert}")

# 3. Build the provider and resolve your top-level service
provider = services.build_service_provider()
notifier = provider.get_service(NotifierService) # Type is inferred!

# 4. Use your services
notifier.notify_admin("System critical!")
# Output: Sending email: Admin Alert: System critical!

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 terms of the LICENSE file included in the repository.

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.0.tar.gz (91.6 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.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wd_di-0.2.0.tar.gz
Algorithm Hash digest
SHA256 19a8ed9d8b1ca7f1b012ae7c76ac3869ccf0e03122537520cce28a5a3a689802
MD5 0fa919ce24f30859b8c634b55b33326b
BLAKE2b-256 55dbab61892c65496e4e1f9d928ac8013d49dd78fda7893b106570c8acc04dd8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for wd_di-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a277f5e98ccf86cbd734506b5f76c6faeff3ea6d03c116f3cef461452f694793
MD5 6fca653b8dd5b1f9a30559189a761a9c
BLAKE2b-256 25f80b206c18e069b89e04fbb604e8bb69275b720e7970256b04b7c058b62937

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