Skip to main content

Dependency Injection Container Micro-framework for implementing IoC

Project description


Dependency Injection Container for Python 3+ influenced partially by Autofac. dic aims to be a tiny framework to help you manage dependencies.

dic uses Python 3 annotations to provide hints for the components that should be injected.


dic is available via pip:
pip install dic


Currently, dic supports:

  1. Constructor injection for classes

  2. Factory and Lazy relationships

  3. Registration via:
    1. Constructor matching for a registered class

    2. Custom callback

  4. Lifetime scopes:
    1. Instance per dependency

    2. Single instance

Quick Example

A quick example on how to use dic:
import dic

class SimpleThing(object):
    def say(self, message):

class RequiresThing(object):
    def __init__(self, thing: SimpleThing):
        self.thing = thing

    def say(self, message):

# build the container
builder = dic.container.ContainerBuilder()

builder.register_class(RequiresThing, component_scope=dic.scope.SingleInstance)

container =

# use the container

# Note there'll only be one of these due to SingleInstance scoping during build
x = container.resolve(RequiresThing)
x.say("my message")


dic supports basic relationships:

  1. dic.rel.Lazy - don’t create the dependency until it’s first used

  2. dic.rel.Factory - the component wants to create other components. Lifetime scopes are respected. Supports custom arguments.

Using a factory:
import dic

class SimpleThing(object):
    def __init__(self, special_argument):
        self.special_argument = special_argument

class BuildsThings(object):
    def __init__(self, thing_factory: dic.rel.Factory(SimpleThing)):
        self.thing_factory = thing_factory

    def build_me_a_thing(self):
        # builds a new thing using the injected factory
        # Note that custom arguments can be provided here
        self.thing_factory(special_argument="My super special argument")

# build the container
builder = dic.container.ContainerBuilder()


container =

# use the container

x = container.resolve(BuildsThing)

# use it
thing = x.build_me_a_thing()
# ...


  1. Is dic thread-safe?

Not yet. There’s plans to make it thread-safe.

  1. Can I define my own scopes?

Yes. Derive a scope from dic.scope.Scope. Scopes can be used to provide lifetime for a calling thread, for example

  1. Will you support feature “X”?

The philosophy of dic is to remain small, but extensible – e.g. remain “out of the way.” So likely not.

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

dic-1.2.0b1.tar.gz (5.6 kB view hashes)

Uploaded Source

Supported by

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