# 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
```
Raw data
{
"_id": null,
"home_page": "https://peter-daly.github.io/clean_ioc/",
"name": "clean_ioc",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "dependency, injection, ioc, inversion of control, type, hints, typing",
"author": "Peter Daly",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/44/6b/c365463752c326f10e1e9550dd9c50d90cd28c8d35ef6bcd366e2972e3ef/clean_ioc-1.9.0.tar.gz",
"platform": null,
"description": "# Clean IoC\nA simple dependency injection library for python that requires nothing of your application code (except that you use typing).\n\n\nRead the [docs](https://peter-daly.github.io/clean_ioc/) to find out more.\n\n\n## Basic Registering and resolving\n\nThere are 4 basic modes of registering a new set of classes\n\n### Implementation\n\n```python\n\nclass UserRepository(abc.ABC):\n @abc.abstractmethod\n def add(self, user):\n pass\n\nclass InMemoryUserRepository(UserRepository):\n\n def __init__(self):\n self.users = []\n\n def add(self, user):\n # This is obviously terrible, but it's for demo purposes\n self.users.append(user)\n\nclass SqlAlchemyUserRepository(UserRepository):\n\n def __init__(self):\n # Do some db stuff here\n pass\n\n def add(self, user):\n # Do some db stuff here\n pass\n\ncontainer = Container()\ncontainer.register(UserRepository, InMemoryUserRepository)\n\n\nrepository = container.resolve(UserRepository) # This will return an InMemoryUserRepository\n\n```\n\n### Concrete Class\n\n```python\n\nclass ClientDependency:\n def get_int(self):\n return 10\n\nclass Client:\n def __init__(self, dep: ClientDependency):\n self.dep = dep\n\n def get_number(self):\n return self.dep.get_int()\n\n\ncontainer = Container()\ncontainer.register(ClientDependency)\ncontainer.register(Client)\n\nclient = container.resolve(Client)\n\nclient.get_number() # returns 10\n\n```\n\n### Factory\n\n```python\n\nclass ClientDependency:\n def get_int(self):\n return 10\n\nclass Client:\n def __init__(self, dep: ClientDependency):\n self.dep = dep\n\n def get_number(self):\n return self.dep.get_int()\n\ndef client_factory(dep: ClientDependency):\n return Client(dep=dep)\n\n\ncontainer = Container()\ncontainer.register(ClientDependency)\ncontainer.register(Client, factory=client_factory)\n\nclient = container.resolve(Client)\n\nclient.get_number() # returns 10\n\n```\n\n\n### Instance\n\n```python\n\nclass ClientDependency:\n def __init__(self, num):\n self.num = num\n\n def get_int(self):\n return self.num\n\nclass Client:\n def __init__(self, dep: ClientDependency):\n self.dep = dep\n\n def get_number(self):\n return self.dep.get_int()\n\nclient_dependency = ClientDependency(num=10)\n\ncontainer = Container()\ncontainer.register(ClientDependency, instance=client_dependency)\ncontainer.register(Client)\n\nclient = container.resolve(Client)\n\nclient.get_number() # returns 10\n\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An IOC Container for Python 3.10+",
"version": "1.9.0",
"project_urls": {
"Documentation": "https://peter-daly.github.io/clean_ioc/",
"Homepage": "https://peter-daly.github.io/clean_ioc/",
"Repository": "https://github.com/peter-daly/clean_ioc"
},
"split_keywords": [
"dependency",
" injection",
" ioc",
" inversion of control",
" type",
" hints",
" typing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ce0116d28ae77f00ef7e902b870a95917d254cce0eabf05131a532d4c472b126",
"md5": "7f702a0d25c61464228dad84457dd542",
"sha256": "8c122d741d8bc9fbcffc312a4311f279489b7aefb1687440ab397839e0d42f95"
},
"downloads": -1,
"filename": "clean_ioc-1.9.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7f702a0d25c61464228dad84457dd542",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 22155,
"upload_time": "2024-10-04T08:27:30",
"upload_time_iso_8601": "2024-10-04T08:27:30.012799Z",
"url": "https://files.pythonhosted.org/packages/ce/01/16d28ae77f00ef7e902b870a95917d254cce0eabf05131a532d4c472b126/clean_ioc-1.9.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "446bc365463752c326f10e1e9550dd9c50d90cd28c8d35ef6bcd366e2972e3ef",
"md5": "7379fbb65f6bdd0324ebbc52b8697966",
"sha256": "5831e54e356153857049489142b6592132f978c9eb471c142933e2723a9de350"
},
"downloads": -1,
"filename": "clean_ioc-1.9.0.tar.gz",
"has_sig": false,
"md5_digest": "7379fbb65f6bdd0324ebbc52b8697966",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 19166,
"upload_time": "2024-10-04T08:27:31",
"upload_time_iso_8601": "2024-10-04T08:27:31.745697Z",
"url": "https://files.pythonhosted.org/packages/44/6b/c365463752c326f10e1e9550dd9c50d90cd28c8d35ef6bcd366e2972e3ef/clean_ioc-1.9.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-04 08:27:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "peter-daly",
"github_project": "clean_ioc",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "clean_ioc"
}