Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Dependency injection

Внедрение зависимостей

With dependency injection (DI), a component receives what it needs from outside instead of creating or looking it up itself. For example, a service can receive a real email sender in production and a test double in a test.

Also known as: DI

What gets injected?

A dependency is something a piece of code needs to do its job. A report exporter might need a place to write the report. If the exporter opens a specific file itself, it decides both what to write and where to write it. With dependency injection (DI), the caller provides a destination. The exporter writes the report without deciding whether it goes to a file or an in-memory buffer.

Where the wiring happens

Required collaborators usually belong in the constructor. That makes the requirements visible to callers and avoids a separate setup step before the object is ready to use. If a dependency is needed only for a particular operation, a method parameter may be a better fit. Setter injection also exists, but using it for a required dependency creates a period when the object exists but is not ready to use.

The code that creates and connects the application's objects often lives near the entry point, in what's called the composition root. You can do this wiring by hand or use a DI container. Containers can resolve a chain of dependencies and manage lifetimes, such as creating one instance per request or reusing one across the application.

A small Python example

from io import StringIO


class ReportExporter:
    def __init__(self, output):
        self.output = output

    def export(self, title):
        self.output.write(f"Report: {title}\n")


buffer = StringIO()
ReportExporter(buffer).export("Sales")
assert buffer.getvalue() == "Report: Sales\n"

The test supplies an in-memory stream and checks the exact output without touching the filesystem. In production, the calling code could pass an open text file instead. That code would also manage the file's lifetime, for example with a with block. None of this requires a framework or a mock library.

Where DI helps and what it doesn't do

DI is especially useful at boundaries: database access, network clients, clocks, and storage. It gives tests control over slow or nondeterministic behavior and keeps infrastructure choices out of business code. It doesn't mean that every object must be injected: creating a local list or a value object inside a method is usually fine.

DI is not the same as a service locator. With a locator, a component asks a registry for what it needs, so callers can't see all its requirements in the signature. DI is also distinct from the Dependency Inversion Principle (DIP) in SOLID. DI is about how a collaborator is supplied; DIP is about making higher-level policy depend on abstractions rather than implementation details. You can inject a concrete class without applying DIP.

What interviewers look for

A typical exercise starts with a service that creates its own database client. Show how to move client creation outside the service and test it with a substitute. If the discussion turns to containers, be ready to explain why an application-wide service should not keep a request-scoped instance: it may use stale request state or try to use an object whose scope has already ended.