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: 8 months 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 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-19T21:19:53.000Z (8 months ago)
- Last Synced: 2025-03-19T22:31:59.897Z (8 months ago)
- Topics: dependency-injection, inversion-of-control, python3, types
- Language: Python
- Homepage: https://peter-daly.github.io/clean_ioc/
- Size: 9.09 MB
- Stars: 11
- Watchers: 2
- 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):
pass
class 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
pass
def add(self, user):
# Do some db stuff here
pass
container = Container()
container.register(UserRepository, InMemoryUserRepository)
repository = container.resolve(UserRepository) # This will return an InMemoryUserRepository
```
### Concrete Class
```python
class ClientDependency:
def get_int(self):
return 10
class Client:
def __init__(self, dep: ClientDependency):
self.dep = dep
def 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 10
class Client:
def __init__(self, dep: ClientDependency):
self.dep = dep
def 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 = num
def get_int(self):
return self.num
class Client:
def __init__(self, dep: ClientDependency):
self.dep = dep
def 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
```