simplistic dependency injection container for python
Project description
# Simple Inversion-Of-Control-Container for python #
This package contains a simple inversion-of-control container. Every injectable element is registered
to the ServiceRegistry. By convention, the class' name with the first letter uncapitalized is used
(UserService -> userService).
## Features ##
* lazy initialization of services (if not registered as object)
* dependency cycle detection
An example:
```python
class Service(object):
# this will make the service registry inject a service named "someOtherService" which
# comes from class SomeOtherService
_someOtherService = None
def __init__(self):
pass
# will be called after everything is injected
def postInit(self):
pass
# will be called right before the object is destroyed (the registry's destroy
# method is called)
def preDestroy(self):
pass
class SomeOtherService(object):
pass
# Use it like
reg = ServiceRegistry()
reg.registerService(Service)
reg.registerService(SomeOtherService)
```
Once the system has all required services registered, a service can be injected by doing
```python
class WiredUser(object):
_service=None
def __init__(self, *args):
pass
wiredUser = reg.createWired(WiredUser, 'arg1', 'arg2')
```
Wired objects are not automatically part of the service registry, only if added by calling `reg.registerServiceInstance`.
They can also wire new objects on the fly:
```python
class WiredUser(object):
_service=None
...
class UserCreator(object):
_serviceRegistry=None
def createUser(self):
return self._serviceRegistry.createWired(WiredUser)
userCreator = reg.createWired(UserCreator)
# create some wired users
userA = userCreator.createUser()
userB = userCreator.createUser()
```
This package contains a simple inversion-of-control container. Every injectable element is registered
to the ServiceRegistry. By convention, the class' name with the first letter uncapitalized is used
(UserService -> userService).
## Features ##
* lazy initialization of services (if not registered as object)
* dependency cycle detection
An example:
```python
class Service(object):
# this will make the service registry inject a service named "someOtherService" which
# comes from class SomeOtherService
_someOtherService = None
def __init__(self):
pass
# will be called after everything is injected
def postInit(self):
pass
# will be called right before the object is destroyed (the registry's destroy
# method is called)
def preDestroy(self):
pass
class SomeOtherService(object):
pass
# Use it like
reg = ServiceRegistry()
reg.registerService(Service)
reg.registerService(SomeOtherService)
```
Once the system has all required services registered, a service can be injected by doing
```python
class WiredUser(object):
_service=None
def __init__(self, *args):
pass
wiredUser = reg.createWired(WiredUser, 'arg1', 'arg2')
```
Wired objects are not automatically part of the service registry, only if added by calling `reg.registerServiceInstance`.
They can also wire new objects on the fly:
```python
class WiredUser(object):
_service=None
...
class UserCreator(object):
_serviceRegistry=None
def createUser(self):
return self._serviceRegistry.createWired(WiredUser)
userCreator = reg.createWired(UserCreator)
# create some wired users
userA = userCreator.createUser()
userB = userCreator.createUser()
```
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
pythonioc-0.2.3.tar.gz
(4.1 kB
view details)
File details
Details for the file pythonioc-0.2.3.tar.gz.
File metadata
- Download URL: pythonioc-0.2.3.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
214b9f17d2ee114f7c8cd1ddc5b531a5c12feeeb6e6d5f359915c882c33e7d5c
|
|
| MD5 |
fc570f16889679e3719317ac015e1ab2
|
|
| BLAKE2b-256 |
1a9652947e232b4c5165584b801b7b3ee11d7942e8f484059825a69d5b02623a
|