Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Inversion of control

Инверсия управления

With this approach, another part of the system decides when to call your code or which objects to give it, instead of leaving those decisions to the code itself.

Also known as: IoC, IoC-контейнер

Who makes the call?

Inversion of control (IoC) moves a specific decision out of the code that does the work. Think of a web route handler: you write the function that builds the response, but the web framework decides when to call it. The framework receives a request, matches it to a route, and invokes the handler you registered. Your function still controls how it builds the response.

Compare that with calling a JSON parser. Your code picks the moment to call the parser and then uses the result. With a route handler, the call goes the other way: the framework calls your code. This is a useful way to explain the difference between a framework and a library, but it's not a strict classification. A library can call callbacks too, so look at who controls the step you're discussing.

Example: a test runner

test('adds two numbers', () => {
  expect(2 + 3).toBe(5);
});

In Jest, test(...) registers a test function. The runner later invokes it, or leaves it out if it doesn't match a test filter. You write the assertion; the runner controls which tests run and reports the results. The callback illustrates IoC without any dependency injection or object container.

Where dependency injection fits

The decision you move out can also be which objects to use. Consider a report service that needs somewhere to store its output. If it creates its own database-backed store, the service picks that implementation. If startup code creates the store and passes it to the service's constructor, the service receives its dependency from outside. That's dependency injection (DI). A unit test can pass an in-memory store without changing the report logic or connecting to a database.

An IoC container is a tool for assembling objects, not another name for the principle. A container can look up the registered implementation for a dependency, create the objects it needs, and reuse them according to their configured lifetimes. This saves repetitive setup when many services depend on one another, but it can make it harder to see where a given object came from. Constructor injection works just as well without a container if you assemble the objects yourself.

How it comes up in interviews

A common follow-up is whether DI and the dependency inversion principle (DIP, the D in SOLID) are the same thing. They're not. DI is about how an object receives a dependency; DIP is about what the code depends on. If a constructor explicitly requires a concrete DatabaseStore, passing one in is still DI. To keep the report logic from depending on that database implementation, have it depend on a storage abstraction that DatabaseStore implements.