warming up your workspace

Dependency injection

Applies to: general

Dependency injection is passing a component's dependencies in from outside rather than creating them inside. It decouples code from concrete implementations, which makes it easier to swap, configure, and test (for example by injecting a fake database).

def report(db):        # db is injected
    return db.query(...)
report(real_db)        # or report(fake_db) in a test

See also: function, api