-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/master' into django-integration
- Loading branch information
Showing
26 changed files
with
234 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
Wireup supports registering the same class multiple times under different qualifiers through the use of factories. | ||
|
||
A common use case for this would be to have multiple services connected to resources of the same underlying | ||
type, such as maintaining multiple database connections: a main and a readonly copy. | ||
|
||
## Example | ||
|
||
Assume an application which has two databases set up: A main one and a readonly replica. In these scenarios the main | ||
connection is used for writes while the readonly connection will be used to perform reads. | ||
|
||
### Service registration via factories | ||
|
||
```python title="db_service.py" | ||
from typing import Annotated | ||
from wireup import container, Wire | ||
|
||
# Define a class that holds the base methods for interacting with the db. | ||
class DatabaseService: | ||
def __init__(self, dsn: str) -> None: | ||
self.__connection = ... | ||
|
||
def query(self) -> ...: | ||
return self.__connection.query(...) | ||
|
||
|
||
# Define a factory which creates and registers the service interacting with the main db. | ||
# Register this directly without using a qualifier, this will be injected | ||
# when services depend on DatabaseService. | ||
@container.register | ||
def main_db_connection_factory( | ||
dsn: Annotated[str, Wire(param="APP_DB_DSN")] | ||
) -> DatabaseService: | ||
return DatabaseService(dsn) | ||
|
||
# This factory registers the function using the qualifier "read" | ||
# and requests the parameter that corresponds to the read replica DSN. | ||
@container.register(qualifier="read") | ||
def read_db_connection_factory( | ||
dsn: Annotated[str, Wire(param="APP_READ_DB_DSN")] | ||
) -> DatabaseService: | ||
return DatabaseService(dsn) | ||
``` | ||
|
||
### Usage | ||
|
||
```python title="thing_repository.py" | ||
from dataclasses import dataclass | ||
from wireup import container | ||
|
||
@container.register | ||
@dataclass | ||
class ThingRepository: | ||
# Main db connection can be injected directly as it is registered | ||
# without a qualifier, this makes it the "default" implementation. | ||
main_db_connection: DatabaseService | ||
|
||
# To inject the read connection the qualifier must be specified. | ||
read_db_connection: Annotated[DatabaseService, Wire(qualifier="read")] | ||
|
||
|
||
def create_thing(self, ...) -> None: | ||
return self.main_db_connection... | ||
|
||
def find_by_id(self, pk: int) -> Thing: | ||
return self.read_db_connection... | ||
``` | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "wireup" | ||
version = "0.7.1" | ||
version = "0.7.2" | ||
description = "Python Dependency Injection Library" | ||
authors = ["Aldo Mateli <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -84,6 +84,9 @@ lint.ignore = [ | |
"COM812", | ||
"ISC001" | ||
] | ||
[tool.ruff.lint.per-file-ignores] | ||
"test/*" = ["D", "ANN", "PT", "SLF001", "T201", "EM101", "TRY", "FA100", "B008", "RUF009", "F401", "SIM117"] | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from dataclasses import dataclass | ||
|
||
from test.unit.services.no_annotations.db_service import DbService | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.