From f5223e7b9434fd502473845512627ac822779986 Mon Sep 17 00:00:00 2001 From: "Christopher A. Flores" Date: Sun, 14 Apr 2024 14:43:35 -0600 Subject: [PATCH] fix: add examples and update documentation --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 853056d..baeca8b 100644 --- a/README.md +++ b/README.md @@ -193,3 +193,32 @@ if __name__ == '__main__': ) ``` +@inject(alias={ 'logger': 'ILogger' }, only=['logger'], exclude=['x']) +The `@inject` decorator also accepts the next parameters: + +### Alias + +The `@inject` decorator use the typing to resolve the required dependency. With the `alias: dict[str, Callable[..., Any]]` parameter you can specify a different implementation for the same interface. For example, let's say we have a UserRepository interface and then two different implementations; UserRepositoryInMemory and UserRepositorySQL. + +```python3 +Provider.set(UserRepository, UserRepositoryInMemory) +Provider.set('UserRepositorySQL', UserRepositorySQL) + +@inject +class CreateUserAccount: + + user: UserRepository +``` + +By default, the `@inject` will use the `UserRepositoryInMemory` to provide the dependency. Let's specify the `UserRepositorySQL` as the provider. To accomplish that we just need to specify the parameter name that we want to override, and then the Provider Key: + + +```python3 +Provider.set(UserRepository, UserRepositoryInMemory) +Provider.set(UserRepositorySQL, UserRepositorySQL) + +@inject(alias = { 'user': 'UserRepositorySQL' }) +class CreateUserAccount: + + user: UserRepository +```