A dependency injection library for Python.
Project description
py-dependency-injection
A dependency injection library for Python.
Features
- Scoped Registrations: Define the lifetime of your dependencies as transient, scoped, or singleton.
- Constructor Injection: Automatically resolve and inject dependencies when creating instances.
- Method Injection: Inject dependencies into methods using a simple decorator.
- Factory Functions: Register factory functions, classes, or lambdas to create dependencies.
- Instance Registration: Register existing instances as dependencies.
- Tag-Based Registration and Resolution: Organize and resolve dependencies based on tags.
- Multiple Containers: Support for using multiple dependency containers.
Compatibility
The library is compatible with the following Python versions:
- 3.7, 3.8, 3.9, 3.10, 3.11, 3.12
Installation
$ pip install py-dependency-injection
Quick Start
Here's a quick example to get you started:
from dependency_injection.container import DependencyContainer
# Define an abstract Connection
class Connection:
pass
# Define a specific implementation of the Connection
class PostgresConnection(Connection):
def connect(self):
print("Connecting to PostgreSQL database...")
# Define a repository that depends on some type of Connection
class UserRepository:
def __init__(self, connection: Connection):
self._connection = connection
def fetch_users(self):
self._connection.connect()
print("Fetching users from the database...")
# Get an instance of the (default) DependencyContainer
container = DependencyContainer.get_instance()
# Register the specific connection type as a singleton instance
container.register_singleton(Connection, PostgresConnection)
# Register UserRepository as a transient (new instance every time)
container.register_transient(UserRepository)
# Resolve an instance of UserRepository, automatically injecting the required Connection
user_repository = container.resolve(UserRepository)
# Use the resolved user_repository to perform an operation
user_repository.find_all()
Documentation
For more advanced usage and examples, please visit our readthedocs page.
License
py-dependency-injection
is released under the GPL 3 license. See LICENSE for more details.
Source Code
You can find the source code for py-dependency-injection
on GitHub.
Release Notes
1.0.0-alpha.10 (2024-08-11)
- Tagged Constructor Injection: Introduced support for constructor injection using the
Tagged
,AnyTagged
, andAllTagged
classes. This allows for seamless injection of dependencies that have been registered with specific tags, enhancing flexibility and control in managing your application's dependencies.
1.0.0-alpha.9 (2024-08-08)
- Breaking Change: Removed constructor injection when resolving dataclasses.
- Enhancement: Added dependency container getter and setter for registrations. Also added new
RegistrationSerializer
class for for serializing and deserializing them. These additions provide a more flexible way to interact with the container's registrations.
1.0.0-alpha.8 (2024-06-07)
- Bug Fix: Fixed an issue in the dependency resolution logic where registered constructor arguments were not properly merged with automatically injected dependencies. This ensures that constructor arguments specified during registration are correctly combined with dependencies resolved by the container.
- Documentation Update: The documentation structure has been updated for better organization and ease of understanding.
1.0.0-alpha.7 (2024-03-24)
- Documentation Update: Updated the documentation to provide clearer instructions and more comprehensive examples.
- Preparing for Beta Release: Made necessary adjustments and refinements in preparation for the upcoming first beta release.
1.0.0-alpha.6 (2024-03-23)
- Factory Registration: Added support for registering dependencies using factory functions for dynamic instantiation.
- Instance Registration: Enabled registering existing instances as dependencies.
- Tag-based Registration and Resolution: Introduced the ability to register and resolve dependencies using tags for flexible dependency management.
1.0.0-alpha.5 (2024-03-03)
- Critical Package Integrity Fix: This release addresses a critical issue that affected the packaging of the Python library in all previous alpha releases (1.0.0-alpha.1 to 1.0.0-alpha.4). The problem involved missing source files in the distribution, rendering the library incomplete and non-functional. Users are strongly advised to upgrade to version 1.0.0-alpha.5 to ensure the correct functioning of the library. All previous alpha releases are affected by this issue.
1.0.0-alpha.4 (2024-03-02)
- Constructor Arguments: Support for constructor arguments added to dependency registration.
1.0.0-alpha.3 (2024-03-02)
- Breaking Change: Starting from this version, the
@inject
decorator can only be used on static class methods and class methods. It can't be used on instance methods anymore. - Documentation Update: The documentation has been updated to reflect the new restriction on the usage of the decorator.
1.0.0-alpha.2 (2024-02-27)
- Python Version Support: Added support for Python versions 3.7, 3.9, 3.10, 3.11, and 3.12.
- New Feature: Method Injection with Decorator: Introduced a new feature allowing method injection using the @inject decorator. Dependencies can now be injected into an instance method, providing more flexibility in managing dependencies within class instance methods.
- New Feature: Multiple Containers: Enhanced the library to support multiple containers. Users can now create and manage multiple dependency containers, enabling better organization and separation of dependencies for different components or modules.
- Documentation Update: Expanded and improved the documentation to include details about the newly added method injection feature and additional usage examples. Users can refer to the latest documentation at readthedocs for comprehensive guidance.
1.0.0-alpha.1 (2024-02-25)
- Initial alpha release.
- Added Dependency Container: The library includes a dependency container for managing object dependencies.
- Added Constructor Injection: Users can leverage constructor injection for cleaner and more modular code.
- Added Dependency Scopes: Define and manage the lifecycle of dependencies with support for different scopes.
- Basic Documentation: An initial set of documentation is provided, giving users an introduction to the library.
- License: Released under the GPL 3 license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file py-dependency-injection-1.0.0a10.tar.gz
.
File metadata
- Download URL: py-dependency-injection-1.0.0a10.tar.gz
- Upload date:
- Size: 22.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96fac2ecbf8661538b94643fcad2a9fedfe9c7a94e39affc968b10010dd91790 |
|
MD5 | f21c97be0aaf2f88be7a4d5057c24c3b |
|
BLAKE2b-256 | 213fced8b96b92fb1e0c489be14246a600bb1902b311b4cbb054119db9f5af0e |
File details
Details for the file py_dependency_injection-1.0.0a10-py3-none-any.whl
.
File metadata
- Download URL: py_dependency_injection-1.0.0a10-py3-none-any.whl
- Upload date:
- Size: 23.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa3b6ec4a63e736457b442dfb1ea09cd4483966eb851f0990448e645f640bb18 |
|
MD5 | 7b8949d08bdb4373f21a9994a266f98f |
|
BLAKE2b-256 | 1843d77ce06d9c95906f1d34ea2e6e3bc185a6f256dc437d099cbbe3f7d28d31 |