Skip to main content

Dependency injection and inversion of control and library for simple service handling

Project description

# IOCynergy
Python module for IOC container using dependency injection (uses python 3+ type hints)

## Easy way to initialize your services
```python
from cynergy import container

class TestClass:
pass

class ParentClass:
def __init__(self, test_class: TestClass):
self.test_class = test_class

instance = container.get(ParentClass) # Returns TestClass initialized as singleton

print(type(instance)) # ParentClass
print(type(instance.test_class)) # TestClass
```


## Access to your configuration from any service
```python
from cynergy import container
from cynergy.config import Config, MemoryConfig
from cynergy.attributes import arguments

@arguments(db=Config('db_name'),host=Config('hostname'))
class DbConnector:
def __init__(self, db: str, host: str):
self.db = db
self.host = host

container.initialize(MemoryConfig({
"db_name": "LocalDbName",
"hostname": "localhost"
}))

instance = container.get(DbConnector)

print(instance.db) # LocalDbName
print(instance.host) # localhost

```
** You can implement your own configuration provider (for exmaple you can create DbConfigProvider which provides your settings from the db)

## Manually register special types

```python
from cynergy import container

class Original:
pass

class Other:
pass

container.register_class(Original, Other)

instance = container.get(Original)

print(type(instance)) # Other
```

## Register collection of services
```python
from typing import List
from cynergy import container

class HandlerBase:
pass

class SomeHandler1(HandlerBase):
pass

class SomeHandler2(HandlerBase):
pass

class SomeService:
def __init__(self, handlers: List[HandlerBase]):
self.handlers = handlers

container.register_many(HandlerBase, [SomeHandler1, SomeHandler2])

instance = container.get(SomeService)

print(type(instance.handlers)) # list
print(type(instance.handlers[0])) # SomeHandler1
print(type(instance.handlers[1])) # SomeHandler2
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cynergy-1.1.6.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file cynergy-1.1.6.tar.gz.

File metadata

  • Download URL: cynergy-1.1.6.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cynergy-1.1.6.tar.gz
Algorithm Hash digest
SHA256 2ff3125867aa4c91779438563b786bc63811c8151ef8fbddc45157368364c275
MD5 c36f67cffc47ad5867344d4fa5ab9904
BLAKE2b-256 7055524bb47f2572b72e4afde82552512d4be704cf49df613305871a9da03c64

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page