Skip to main content

simplistic dependency injection container for python

Project description

This package provides a simple inversion-of-control container.

Install with

pip install pythonioc

Quick Start

The main idea is: services are registered to a service-registry and can be injected into users of that service (which can of course be services themselves).

You have two options:

  1. use a global registry (never create a registry yourself)

    –> easy to implement, harder to test

  2. use a local registry

Examples are below, more details will follow.

Global Registry

import pythonioc

# register your service
@pythonioc.Service
class SomeService(object):

    # called when the service is auto-instantiated.
    def postInit(self):
        pass

@pythonioc.NamedService('DifferentNameService')
class DifferentService(object):
    pass

# for classes which we cannot decorate:
pythonioc.registerService(ExternalService)

# when we don't even have the class (or don't care about lazy-initialization)
pythonioc.registerServiceInstance(SomeService())


class ServiceUser(object):
    # inject the dependency by class
    service = pythonioc.Inject(SomeService)

    # inject the dependency by name (for cyclic dependencies)
    service2 = pythonioc.Inject('DifferentNameService')


 myUser = ServiceUser()

 myUser.service # --> automatically created and injected service instance.

 # explicitly get a service
 pythonioc.getService(SomeService)
 pythonioc.getService('DifferentNameService')

Local Registry

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 clean
    # method is called)
    def preDestroy(self):
        pass



class SomeOtherService(object):
    pass

# let's register our services
reg = ServiceRegistry()
reg.registerService(Service)
reg.registerService(SomeOtherService)

Once everything is registered, a service can be injected by

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.

Wired objects can inject its own service registry, so they can created wired Objects on the fly:

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()

Features

  • lazy initialization of services

  • dependency cycle detection

Notes on Names

Services added to the registry need a name. If no name is provided, the class’ name (or the instance’s class’ name) is used. The name’s first character is lowered.

Example:

import pythonioc

class MyService(object):
    pass

pythonioc.registerService(MyService)            # --> name is 'myService'
pythonioc.registerServiceInstance(MyService())  # --> name is 'myService'
pythonioc.registerService(MyService, serviceName='customName') # --> name is 'customName'
pythonioc.registerServiceInstance(MyService(), serviceName='customName2')  # --> name is 'customName2'

Feel free to report any bugs, suggest improvements etc.

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

pythonioc-0.2.7.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file pythonioc-0.2.7.tar.gz.

File metadata

  • Download URL: pythonioc-0.2.7.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pythonioc-0.2.7.tar.gz
Algorithm Hash digest
SHA256 6eab66481252ee1797c0de4ca64767329de7ca32fa3b93395712a427a17b68c3
MD5 c334821711d580a78d82a45b84f38548
BLAKE2b-256 38e1c2961352507cb09c3444d02986b8e3ab56ab02a6b170c81122ba56fe402d

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