Lightweight Dependency Injector Setter Python -10 lines! Tiny DIS in Python.
Latest commit 2c30746 Apr 10, 2011 @liuggio added some text and todo.
Failed to load latest commit information. added some text and todo. Apr 10, 2011 Dependency Setter Apr 9, 2011 Dependency Setter Examples Apr 9, 2011

What is it?

Is just a dependency injector

it's a design pattern for maintain high level on the software quality

** and is not a Service Locator.**

is useful for small-sized projects, when you want to maintain decency in your code without using invasive frameworks

is useful if you want to test the code, to give a mock object dependencies

is useful to understand how work a stupid dependency injector

is useful to reuse the code

is NOT useful if you have a lot of injected object, is better use a Service Locator and a PROPER DIC framework.

++ todo

injection without instantiation, using metaclass ??

What is a Dependency Injection or DI?

How it works?

from di import *

# instance = inject(Class, tuple of parameters)

EmailSender = inject(Mailer, dispatcher=SmtpLibV02)
# EmailSender is now instantiated 
# and has the attribute 'dispatcher' with SmtpLib instantiated


please fork it and add some ideas :)

git clone di
cd di

Few Examples

The code

from types import *
def inject(class_holder, **dependencies):
        if isinstance(class_holder, ClassType):
            istance = class_holder()
        for dependency in dependencies:
            if isinstance(dependencies[dependency], ClassType):
            setattr(istance, dependency, dependencies[dependency])
        return istance