Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/peter-daly/clean_ioc
A simple IoC library for python
https://github.com/peter-daly/clean_ioc
dependency-injection inversion-of-control python3 types
Last synced: 21 days ago
JSON representation
A simple IoC library for python
- Host: GitHub
- URL: https://github.com/peter-daly/clean_ioc
- Owner: peter-daly
- License: mit
- Created: 2023-01-24T22:43:29.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-10T13:23:57.000Z (2 months ago)
- Last Synced: 2024-09-10T14:45:39.684Z (2 months ago)
- Topics: dependency-injection, inversion-of-control, python3, types
- Language: Python
- Homepage: https://peter-daly.github.io/clean_ioc/
- Size: 9.2 MB
- Stars: 9
- Watchers: 1
- Forks: 2
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.rst
- License: LICENSE
Awesome Lists containing this project
- awesome-dependency-injection-in-python - Clean IoC - A simple unintrusive dependency injection library for python with strong support for generics [🐍, MIT License]. (Software / DI Frameworks / Containers)
README
# Clean IoC
A simple dependency injection library for python that requires nothing of your application code (except that you use typing).Read the [docs](https://peter-daly.github.io/clean_ioc/) to find out more.
## Basic Registering and resolving
There are 4 basic modes of registering a new set of classes
### Implementation
```python
class UserRepository(abc.ABC):
@abc.abstractmethod
def add(self, user):
passclass InMemoryUserRepository(UserRepository):
def __init__(self):
self.users = []def add(self, user):
# This is obviously terrible, but it's for demo purposes
self.users.append(user)class SqlAlchemyUserRepository(UserRepository):
def __init__(self):
# Do some db stuff here
passdef add(self, user):
# Do some db stuff here
passcontainer = Container()
container.register(UserRepository, InMemoryUserRepository)repository = container.resolve(UserRepository) # This will return an InMemoryUserRepository
```
### Concrete Class
```python
class ClientDependency:
def get_int(self):
return 10class Client:
def __init__(self, dep: ClientDependency):
self.dep = depdef get_number(self):
return self.dep.get_int()container = Container()
container.register(ClientDependency)
container.register(Client)client = container.resolve(Client)
client.get_number() # returns 10
```
### Factory
```python
class ClientDependency:
def get_int(self):
return 10class Client:
def __init__(self, dep: ClientDependency):
self.dep = depdef get_number(self):
return self.dep.get_int()def client_factory(dep: ClientDependency):
return Client(dep=dep)container = Container()
container.register(ClientDependency)
container.register(Client, factory=client_factory)client = container.resolve(Client)
client.get_number() # returns 10
```
### Instance
```python
class ClientDependency:
def __init__(self, num):
self.num = numdef get_int(self):
return self.numclass Client:
def __init__(self, dep: ClientDependency):
self.dep = depdef get_number(self):
return self.dep.get_int()client_dependency = ClientDependency(num=10)
container = Container()
container.register(ClientDependency, instance=client_dependency)
container.register(Client)client = container.resolve(Client)
client.get_number() # returns 10
```