Dicon, simple DI container injection liblary for Python.
Project description
Dicon, simple DI container injection liblary for Python.
---
Dicon is a simple "DI container injection" library.
## FAQ
- Q. Is `dicon` a dependency injection library?
- A. No, it does not support dependency injection. It supports DI container injection and service (singleton) locator.
## Important points
- Simple to use.
- Clear what it does on each stage.
- Small overhead.
## Features
- `@dicon.inject_di_container` class decorator supports nested DI container injection, without modification on type.
- `dicon.DIContainer` provides class resolver and singleton locating.
### Class resolver, no modification on type
```python
import dicon
class A:
pass
di_container = dicon.DIContainer()
di_container.register[A]()
di_container.freeze()
# Resolve class.
class_A = di_container.resolve[A]
a = class_A()
# dicon.DIContainer does not modify type of the object.
assert type(a) is A
# It only modifies constructor. (Normally it will be not a problem.)
assert not (class_A is A)
```
### Nested DI container injection
```python
@dicon.inject_di_container('_di_container')
class B:
_di_container = None # Here we get instance of DIContainer.
def __init__(self, message):
print(message, self._di_container)
@dicon.inject_di_container('_di_container')
class C:
_di_container = None # Here we get instance of DIContainer.
def __init__(self):
self._b = self._di_container.resolve[B]('B must be have DIContainer:')
di_container = dicon.DIContainer()
di_container.register[B]()
di_container.register[C]()
di_container.freeze()
di_container.resolve[C]()
# will print 'B must be have DIContainer: ...'
```
### Singleton locating
TBW...
## How it works
1. `@dicon.inject_di_container` modifies `__init__` of given class to make it possible `__init__` can take DIContainer as an argument.
2. `DIContainer.resolve` ties itself to that argument by `functools.partial`.
That's all.
For more details, see [tests](tests).
## Other existing solutions
- Dependency injection
- There are lots of DI libraries: for example, [injector](https://github.com/alecthomas/injector), [pinject](https://github.com/google/pinject).
- IMHO, DI is too complex for python.
- Famous liblaries are too complex and not clear how affect to the program.
- It seems no one support nested injection.
- Service locator
- [wired](https://github.com/mmerickel/wired)
- I personally felt it is too complex.
- [factoryfactory](https://github.com/jammycakes/factoryfactory)
- It does not support multiple service locators.
I borrowed design of [MicroResolver](https://github.com/neuecc/MicroResolver) in API.
---
Dicon is a simple "DI container injection" library.
## FAQ
- Q. Is `dicon` a dependency injection library?
- A. No, it does not support dependency injection. It supports DI container injection and service (singleton) locator.
## Important points
- Simple to use.
- Clear what it does on each stage.
- Small overhead.
## Features
- `@dicon.inject_di_container` class decorator supports nested DI container injection, without modification on type.
- `dicon.DIContainer` provides class resolver and singleton locating.
### Class resolver, no modification on type
```python
import dicon
class A:
pass
di_container = dicon.DIContainer()
di_container.register[A]()
di_container.freeze()
# Resolve class.
class_A = di_container.resolve[A]
a = class_A()
# dicon.DIContainer does not modify type of the object.
assert type(a) is A
# It only modifies constructor. (Normally it will be not a problem.)
assert not (class_A is A)
```
### Nested DI container injection
```python
@dicon.inject_di_container('_di_container')
class B:
_di_container = None # Here we get instance of DIContainer.
def __init__(self, message):
print(message, self._di_container)
@dicon.inject_di_container('_di_container')
class C:
_di_container = None # Here we get instance of DIContainer.
def __init__(self):
self._b = self._di_container.resolve[B]('B must be have DIContainer:')
di_container = dicon.DIContainer()
di_container.register[B]()
di_container.register[C]()
di_container.freeze()
di_container.resolve[C]()
# will print 'B must be have DIContainer: ...'
```
### Singleton locating
TBW...
## How it works
1. `@dicon.inject_di_container` modifies `__init__` of given class to make it possible `__init__` can take DIContainer as an argument.
2. `DIContainer.resolve` ties itself to that argument by `functools.partial`.
That's all.
For more details, see [tests](tests).
## Other existing solutions
- Dependency injection
- There are lots of DI libraries: for example, [injector](https://github.com/alecthomas/injector), [pinject](https://github.com/google/pinject).
- IMHO, DI is too complex for python.
- Famous liblaries are too complex and not clear how affect to the program.
- It seems no one support nested injection.
- Service locator
- [wired](https://github.com/mmerickel/wired)
- I personally felt it is too complex.
- [factoryfactory](https://github.com/jammycakes/factoryfactory)
- It does not support multiple service locators.
I borrowed design of [MicroResolver](https://github.com/neuecc/MicroResolver) in API.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dicon-0.0.3.tar.gz
(8.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
dicon-0.0.3-py3-none-any.whl
(4.6 kB
view details)
File details
Details for the file dicon-0.0.3.tar.gz.
File metadata
- Download URL: dicon-0.0.3.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.0 pkginfo/1.4.2 requests/2.19.1 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.14.0 CPython/3.6.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30fbb747c8e17b0150032e85c2b20cbcee8e84153b6dbfbbfc8d3e368f663973
|
|
| MD5 |
a95a76422d69381eb108201ae0d7fca9
|
|
| BLAKE2b-256 |
fc0319acbd77ecc17b345b33232ab69fad7190cbe031eadbf795954f85e5c2ec
|
File details
Details for the file dicon-0.0.3-py3-none-any.whl.
File metadata
- Download URL: dicon-0.0.3-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.0 pkginfo/1.4.2 requests/2.19.1 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.14.0 CPython/3.6.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
035d559875dde50429c10fd8b04250eb90a8cf8ad1abb97724161ad74a552f09
|
|
| MD5 |
6f9557fdab7e961ae7e3c251aba0628a
|
|
| BLAKE2b-256 |
1685c9361a94b3747a2c8e22c75be866e7db62f78d0be01c6581a872c8d93a22
|