Skip to content

Commit

Permalink
fix: add examples and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cafadev committed Apr 14, 2024
1 parent 750f662 commit f5223e7
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit f5223e7

Please sign in to comment.