Skip to content

Commit

Permalink
readme/quickstart update
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Sep 11, 2023
1 parent bb2326e commit 039c061
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
20 changes: 7 additions & 13 deletions docs/pages/quickstart.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The following is a short example demonstrating a simple application with two services (classes that provide
functionality) and parameters (app configuration) showing automatic dependency resolution and injection (autowiring)
Demonstration of a simple application with two services (classes that provide functionality)
and parameters (app configuration) showing automatic dependency resolution and injection (autowiring)
for services and a simple web view called "greet".

**1. Register dependencies**
Expand All @@ -14,20 +14,14 @@ container.params.update({
})


# Constructor injection is supported for regular classes as well as dataclasses.
@container.register # (2)!
class DbService:
def __init__(
self,
# Inject a parameter by name
connection_str: str = wire(param="db.connection_str"),
# Or by interpolating multiple parameters into a string
cache_dir: str = wire(expr="${cache_dir}/${env}/db"),
):
self.connection_str = connection_str
self.cache_dir = cache_dir
# Inject a parameter by name
connection_str: str = wire(param="db.connection_str"),
# Or by interpolating multiple parameters into a string
cache_dir: str = wire(expr="${cache_dir}/${env}/db"),


# Constructor injection is also supported for dataclasses
# resulting in a more compact syntax.
@container.register
@dataclass
Expand Down
50 changes: 30 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,38 @@
Dependency injection library designed to provide a powerful and flexible way to manage and inject
dependencies making it easier to develop, test, and maintain Python codebases.

---
## Quickstart guide

Demonstration of a simple application with two services (classes that provide functionality)
and parameters (app configuration) showing automatic dependency resolution and injection (autowiring)
for services and a simple web view called "greet".

**1. Register dependencies**

```python
from wireup import container

# Parameters are configuration for your services.
# Think of a database url or environment name.
container.params.update({
"db.connection_str": os.environ.get("DATABASE_URL")
"cache_dir": gettempdir(),
"env": os.environ.get("ENV", "dev")
})


@container.register
# Register a class as a dependency in the container.
# Constructor injection is supported for regular classes as well as dataclasses.
# In turn this can also depend on other objects in the container.
@container.register
class DbService:
def __init__(
self,
# Inject a parameter by name
connection_str: str = wire(param="db.connection_str"),
# Or by interpolating multiple parameters into a string
cache_dir: str = wire(expr="${cache_dir}/${env}/db"),
):
self.connection_str = connection_str
self.cache_dir = cache_dir


# Constructor injection is also supported for dataclasses
# resulting in a more compact syntax.
# Inject a parameter by name
connection_str: str = wire(param="db.connection_str"),
# Or by interpolating multiple parameters into a string
cache_dir: str = wire(expr="${cache_dir}/${env}/db"),


@container.register
@dataclass
class UserRepository:
Expand All @@ -39,16 +44,21 @@ class UserRepository:
**2. Inject**

```python
# Decorate all methods where the library must perform injection.
@app.route("/greet/<str:name>")
@container.autowire
# Decorate all targets where the library must perform injection,such as views in a web app.
# Classes are automatically injected based on annotated type.
# Parameters will be located based on the hint given in their default value.
# Unknown arguments will not be processed.
def greet(name: str, user_repository: UserRepository, env: str = wire(param="env")):
...
def greet(name: str, user_repository: UserRepository, env: str = wire(param="env")):
...
```

## Documentation
**Installation**

```bash
# Install using poetry:
poetry add wireup

For more information [refer to the documentation](https://maldoinc.github.io/wireup/)
# Install using pip:
pip install wireup
```

0 comments on commit 039c061

Please sign in to comment.