From 039c0611ba042e2958b76125e92a05511c3e7ab1 Mon Sep 17 00:00:00 2001 From: Aldo Mateli Date: Mon, 11 Sep 2023 22:38:55 +0100 Subject: [PATCH] readme/quickstart update --- docs/pages/quickstart.md | 20 ++++++---------- readme.md | 50 ++++++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/docs/pages/quickstart.md b/docs/pages/quickstart.md index 18e647f..bee0a30 100644 --- a/docs/pages/quickstart.md +++ b/docs/pages/quickstart.md @@ -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** @@ -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 diff --git a/readme.md b/readme.md index 8fb5fec..72fbc03 100644 --- a/readme.md +++ b/readme.md @@ -3,11 +3,20 @@ 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(), @@ -15,21 +24,17 @@ container.params.update({ }) -@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: @@ -39,16 +44,21 @@ class UserRepository: **2. Inject** ```python -# Decorate all methods where the library must perform injection. @app.route("/greet/") @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 +```