Skip to content

Commit f4341c4

Browse files
committed
Make README more clean, add 'why' section.
1 parent 41b792c commit f4341c4

File tree

1 file changed

+34
-87
lines changed

1 file changed

+34
-87
lines changed

README.md

Lines changed: 34 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
[![License](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.html)
2-
![First Principles Software](https://img.shields.io/badge/Powered_by-First_Principles_Software-blue)
2+
[![Author: David Runemalm](https://img.shields.io/badge/Author-David%20Runemalm-blue)](https://www.davidrunemalm.com)
33
[![Master workflow](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml/badge.svg?branch=master)](https://github.com/runemalm/py-dependency-injection/actions/workflows/master.yml)
4+
[![PyPI version](https://badge.fury.io/py/py-dependency-injection.svg)](https://pypi.org/project/py-dependency-injection/)
5+
![Downloads](https://pepy.tech/badge/py-dependency-injection)
46

57
# py-dependency-injection
68

79
A dependency injection library for Python.
810

11+
## Why py-dependency-injection?
12+
13+
`py-dependency-injection` is inspired by the built-in dependency injection system in **ASP.NET Core**. It provides a lightweight and extensible way to manage dependencies in Python applications. By promoting constructor injection and supporting scoped lifetimes, it encourages clean architecture and makes testable, maintainable code the default.
14+
915
## Features
1016

1117
- **Scoped Registrations:** Define the lifetime of your dependencies as transient, scoped, or singleton.
@@ -35,38 +41,36 @@ Here's a quick example to get you started:
3541
```python
3642
from dependency_injection.container import DependencyContainer
3743

38-
# Define an abstract Connection
39-
class Connection:
40-
pass
44+
# Define an abstract payment gateway interface
45+
class PaymentGateway:
46+
def charge(self, amount: int, currency: str):
47+
raise NotImplementedError()
4148

42-
# Define a specific implementation of the Connection
43-
class PostgresConnection(Connection):
44-
def connect(self):
45-
print("Connecting to PostgreSQL database...")
49+
# A concrete implementation using Stripe
50+
class StripeGateway(PaymentGateway):
51+
def charge(self, amount: int, currency: str):
52+
print(f"Charging {amount} {currency} using Stripe...")
4653

47-
# Define a repository that depends on some type of Connection
48-
class UserRepository:
49-
def __init__(self, connection: Connection):
50-
self._connection = connection
54+
# A service that depends on the payment gateway
55+
class CheckoutService:
56+
def __init__(self, gateway: PaymentGateway):
57+
self._gateway = gateway
5158

52-
def fetch_users(self):
53-
self._connection.connect()
54-
print("Fetching users from the database...")
59+
def checkout(self):
60+
self._gateway.charge(2000, "USD") # e.g. $20.00
5561

56-
# Get an instance of the (default) DependencyContainer
62+
# Get the default dependency container
5763
container = DependencyContainer.get_instance()
5864

59-
# Register the specific connection type as a singleton instance
60-
container.register_singleton(Connection, PostgresConnection)
65+
# Register StripeGateway as a singleton (shared for the app's lifetime)
66+
container.register_singleton(PaymentGateway, StripeGateway)
6167

62-
# Register UserRepository as a transient (new instance every time)
63-
container.register_transient(UserRepository)
68+
# Register CheckoutService as transient (new instance per resolve)
69+
container.register_transient(CheckoutService)
6470

65-
# Resolve an instance of UserRepository, automatically injecting the required Connection
66-
user_repository = container.resolve(UserRepository)
67-
68-
# Use the resolved user_repository to perform an operation
69-
user_repository.find_all()
71+
# Resolve and use the service
72+
checkout = container.resolve(CheckoutService)
73+
checkout.checkout()
7074
```
7175

7276
## Documentation
@@ -81,69 +85,12 @@ For more advanced usage and examples, please visit our [readthedocs](https://py-
8185

8286
You can find the source code for `py-dependency-injection` on [GitHub](https://github.com/runemalm/py-dependency-injection).
8387

84-
## Release Notes
85-
86-
### [1.0.0-beta.2](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-beta.2) (2025-06-09)
87-
88-
- **Enhancement**: Constructor parameters with default values or `Optional[...]` are now supported without requiring explicit registration.
89-
- **Python Version Support**: Added support for Python version 3.13.
90-
91-
### [1.0.0-beta.1](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-beta.1) (2025-01-06)
92-
93-
- **Transition to Beta**: Transitioned from alpha to beta. Features have been stabilized and are ready for broader testing.
94-
- **Removal**: We have removed the dependency container getter and setter functions, as well as the RegistrationSerializer class, which were first introduced in v1.0.0-alpha.9. This decision reflects our focus on maintaining a streamlined library that emphasizes core functionality. These features, which would not be widely used, added unnecessary complexity without offering significant value. By removing them, we are reinforcing our commitment to our design principles.
95-
- **Enhancement**: Added suppprt for configuring default scope name. Either a static string value, or a callable that returns the name.
96-
97-
### [1.0.0-alpha.10](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.10) (2024-08-11)
98-
99-
- **Tagged Constructor Injection**: Introduced support for constructor injection using the `Tagged`, `AnyTagged`, and `AllTagged` 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.
100-
101-
### [1.0.0-alpha.9](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.9) (2024-08-08)
102-
103-
- **Breaking Change**: Removed constructor injection when resolving dataclasses.
104-
- **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.
105-
106-
### [1.0.0-alpha.8](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.8) (2024-06-07)
10788

108-
- **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.
109-
- **Documentation Update**: The documentation structure has been updated for better organization and ease of understanding.
110-
111-
### [1.0.0-alpha.7](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.7) (2024-03-24)
112-
113-
- **Documentation Update**: Updated the documentation to provide clearer instructions and more comprehensive examples.
114-
- **Preparing for Beta Release**: Made necessary adjustments and refinements in preparation for the upcoming first beta release.
115-
116-
### [1.0.0-alpha.6](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.6) (2024-03-23)
117-
118-
- **Factory Registration**: Added support for registering dependencies using factory functions for dynamic instantiation.
119-
- **Instance Registration**: Enabled registering existing instances as dependencies.
120-
- **Tag-based Registration and Resolution**: Introduced the ability to register and resolve dependencies using tags for flexible dependency management.
121-
122-
### [1.0.0-alpha.5](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.5) (2024-03-03)
123-
124-
- **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.
125-
126-
### [1.0.0-alpha.4](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.4) (2024-03-02)
127-
128-
- **Constructor Arguments**: Support for constructor arguments added to dependency registration.
129-
130-
### [1.0.0-alpha.3](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.3) (2024-03-02)
131-
132-
- **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.
133-
- **Documentation Update**: The documentation has been updated to reflect the new restriction on the usage of the decorator.
134-
135-
### [1.0.0-alpha.2](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.2) (2024-02-27)
89+
## Release Notes
13690

137-
- **Python Version Support**: Added support for Python versions 3.7, 3.9, 3.10, 3.11, and 3.12.
138-
- **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.
139-
- **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.
140-
- **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.
91+
### Latest: [1.0.0-beta.3](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-beta.3) (2025-06-14)
14192

142-
### [1.0.0-alpha.1](https://github.com/runemalm/py-dependency-injection/releases/tag/v1.0.0-alpha.1) (2024-02-25)
93+
- **Enhancement**: Added `DependencyContainer.configure_default_container_name(...)` to support container isolation in parallel tests, even when application code uses a single shared container via `DependencyContainer.get_instance()`.
94+
- **Enhancement**: Added `DependencyContainer.clear_instances()` as a clean alternative to manually resetting `_instances` during test teardown.
14395

144-
- **Initial alpha release**.
145-
- **Added Dependency Container**: The library includes a dependency container for managing object dependencies.
146-
- **Added Constructor Injection**: Users can leverage constructor injection for cleaner and more modular code.
147-
- **Added Dependency Scopes**: Define and manage the lifecycle of dependencies with support for different scopes.
148-
- **Basic Documentation**: An initial set of documentation is provided, giving users an introduction to the library.
149-
- **License**: Released under the GPL 3 license.
96+
➡️ Full changelog: [GitHub Releases](https://github.com/runemalm/py-dependency-injection/releases)

0 commit comments

Comments
 (0)