Skip to main content
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.


Release files for dicon 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dicon 0.0.3
File Size Uploaded
dicon-0.0.3.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dicon 0.0.3
File Interpreter ABI Platform
dicon-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.7 kB

Release files / dicon-0.0.3.tar.gz

Download URL dicon-0.0.3.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
30fbb747c8e17b0150032e85c2b20cbcee8e84153b6dbfbbfc8d3e368f663973
BLAKE2b-256 checksum
How to use checksums
fc0319acbd77ecc17b345b33232ab69fad7190cbe031eadbf795954f85e5c2ec
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / dicon-0.0.3-py3-none-any.whl

Download URL dicon-0.0.3-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
035d559875dde50429c10fd8b04250eb90a8cf8ad1abb97724161ad74a552f09
BLAKE2b-256 checksum
How to use checksums
1685c9361a94b3747a2c8e22c75be866e7db62f78d0be01c6581a872c8d93a22
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page